Method Hiding
A static method (class method) cannot be overridden in Java. But if a static method
defined in the parent class is redefined in a child class, the child class’s method
hides the method defined in the parent class.
This mechanism is called method hiding in Java or function hiding. The syntax to
call hidden static method in a Java program is as follows:
Syntax:
superClassName.staticMethodName
Rules of Method Hiding in Java
All rules of method hiding are exactly the same as overriding except one rule.
1. Both parent and child class methods must be static.
Features of Method Hiding in Java
1. Method hiding is also known as compile-time polymorphism because the
compiler is responsible to resolve method resolution based on the reference type.
2. It is also known as static polymorphism or early binding.
3. In method hiding, method call is always resolved by Java compiler based on the
reference type. There is no role of runtime polymorphism in method hiding in java.
4. The use of static in method declaration must be the same between superclass and
subclass.
Difference between Method Hiding and Method Overriding
There are the following differences between method hiding and method overriding
in Java. They are:
1. In method hiding, both parent and child class methods should be static whereas,
in overriding, both parent and child class methods should be non-static.
2. Compiler is responsible for method resolution based on reference type whereas,
in method overriding, JVM is always responsible for method resolution based on
runtime object.
3. Method hiding is also known as compile-time polymorphism, static
polymorphism, or early binding whereas, method overriding is also known as
runtime polymorphism, dynamic polymorphism, or late binding.
Method Hiding and Method Overriding Example Program
public class ParentClass
public static void classMethod()
System.out.println("classMethod in Parent class");
public void instanceMethod()
System.out.println("instanceMethod in Parent class");
}
public class ChildClass extends ParentClass
public static void classMethod()
System.out.println("classMethod in Child class");
public void instanceMethod()
System.out.println("instanceMethod in Child class");
public class MyClass
public static void main(String[] args)
ParentClass p = new ChildClass();
p.classMethod(); // Calling with reference. (Method hiding)
p.instanceMethod(); // Calling with object. (Method overriding)
ChildClass c = new ChildClass();
c.classMethod(); // Calling with reference.
c.instanceMethod(); // Calling with object.
ParentClass p1=new ParentClass();
p1.classMethod(); // Calling with reference.
p1.instanceMethod(); // Calling with object.
Output:
classMethod in Parent class
instanceMethod in Child class
classMethod in Child class
instanceMethod in Child class
classMethod in Parent class
instanceMethod in Parent class
Key points:
1. In method overriding, method call is resolved by JVM based on runtime object
(new Object)
2. In method hiding, method call is resolved by the compiler based on reference
type (Object obj).
===================
Program source code 2:
public class X
protected static void m1(int a)
System.out.println("m1-X");
public class Y extends X
static void m1(int y)
System.out.println("m1-Y");
public class MyTest
public static void main(String[] args)
X x = new Y();
x.m1(10);
Y y = new Y();
y.m1(20);
}
}
Output:
m1-X
Compile-time error
Explanation:
1. x.m1(10); will call m1() method of class X because x is a reference type of X.
2. y.m1(20); will give a compile-time error because we cannot reduce the visibility
of the inherited method from X.
Difference between Method Hiding and Method Overriding
There are the following differences between method hiding and method overriding
in Java. They are:
1. In method hiding, both parent and child class methods should be static whereas,
in overriding, both parent and child class methods should be non-static.
2. Compiler is responsible for method resolution based on reference type whereas,
in method overriding, JVM is always responsible for method resolution based on
runtime object.
3. Method hiding is also known as compile-time polymorphism, static
polymorphism, or early binding whereas, method overriding is also known as
runtime polymorphism, dynamic polymorphism, or late binding.