Method Hiding in Java10 May 2025 | 4 min read In this section, we will discuss what is method hiding in Java, method hiding factors (MHF), and the differences between method hiding and method overriding. Also, implement the method hiding concept in a Java program. To understand the method hiding concept in Java, first, we will understand the method overriding. Because the method hiding concept is very similar to the method overriding. What is method overriding?Method overriding means subclass had defined an instance method with the same signature and return type as the instance method in the superclass. In such a case, method of the superclass is overridden (replaced) by the subclass. Methods in Static ContextStatic methods are bonded during compile time using types of reference variables not object. We know that static methods are accessed by using the class name rather than an object. Note that the static method can be overloaded, but cannot be overridden in Java. What is method hiding?Method hiding can be defined as, "if a subclass defines a static method with the same signature as a static method in the super class, in such a case, the method in the subclass hides the one in the superclass." The mechanism is known as method hiding. It happens because static methods are resolved at compile time. Method Hiding Factors (MHF)
![]() Example of Method Hiding in JavaSample.java Output: Method-1 of the Demo class is executed. Method-1 of the Demo class is executed. Method-2 of the Demo class is executed. Method-2 of the Sample class is executed. We observe that the method of the superclass is hidden by the subclass. Method Hiding Vs. Method OverridingHiding a static method of a superclass looks like overriding an instance method of a superclass. The main difference can be seen at runtime in the following scenario.
Let's understand the method handing and overriding practically. The above code snippet does not perform the method hiding because the methods of both the classes are non-static, hence they perform method overriding. In the above code snippet, to achieve the method hiding, we need to make the methods static. The following table describes what happens when we define a method with the same signature as in superclass.
Note: In a subclass, we can overload the methods inherited from the superclass. The overloaded methods neither hide nor override the superclass instance methods. These are the new methods, that are unique to the subclass.Next TopicLinked-list-components-in-java | ||||||||||||||||||||
nth Prime Number Java
A number is prime if it is divisible by 1 and itself. In other words, a prime number is a natural number with exactly two distinct natural number divisors 1 and itself. For example, 2, 3, 5, 7, 11, etc. are the prime numbers. Note that...
5 min read
Java Program to Find Distance of Nearest Cell Having 1 in a Binary Matrix
Problem Statement With reference to a given binary matrix (it is a grid in which each cell holds nothing but either the number 0 or 1), the assignment is to determine the smallest number of moves needed to get from the 0 cell to the 1 cell....
7 min read
How to call a concrete method of abstract class in Java
The ability to call concrete methods of abstract classes is an important concept to understand when programming in Java. Abstract classes provide a way to define a standard set of behaviours and features that multiple classes can share without implementing them from scratch. These classes can...
5 min read
Building a Search Engine in Java
Search engines play a pivotal role in today's digital world, enabling users to find relevant information quickly and efficiently. While creating a full-scale search engine like Google is a massive undertaking, you can build a basic search engine in Java to search through a collection of...
6 min read
Package class
Java The package class provides methods to get information about the specification and implementation of a package. It provides methods such as getName(), getImplementationTitle(), getImplementationVendor(), getImplementationVersion() etc. Example of In this example, we are printing the details of java.lang package by invoking the methods of package...
1 min read
Sum of Numbers with Units Digit K in Java
The task of finding numbers whose units digit is equal to k and their sum is equal to the given num is an interesting computational problem which can be solved using different approaches in Java. Example 1 Input num = 58, k = 9 Output: 2...
7 min read
Find the Longest Common Prefix Using Binary Search in Java
The Longest Common Prefix (LCP) problem stands for finding the longest string, which is a prefix of all strings in a given list; this is a classical problem for computer science that has many applications, for example, in DNA sequence analysis, auto-completion, data compression, and...
5 min read
Delete Mid of a Stack in Java
Stackers are linear data structures in principle. A simple Load-In-First-Out (LIFO) set is the last item added to the stack and the first item to be removed. The basic operations in a stack include push, pop, and peek. However, manipulating the middle element of a stack-such...
5 min read
Lock Interface in Java
The lock interface is one of the most used interfaces in Java. Lock interface is available in the Java.util.concurrent.locks package which we use as a thread synchronization mechanism, i.e., similar to synchronized blocks. It is more flexible and provides more options in comparison to the synchronized...
4 min read
Fermat's Little Theorem in Java
Fermat's Little Theorem is a fundamental contribution to the concept of quantity that is widely used in arithmetic and computer science. It is especially true in discrete arithmetic, cryptography, and basic verification. Named after the French mathematician Pierre de Fermat, the theory states a key property of...
5 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India
