What is method hiding in Java and how to use it?


When super class and sub class contains same method including parameters and if they are static.

The method in the super class will be hidden by the one that is in the sub class. This mechanism is known as method hiding.

Example

Live Demo

class Demo{
   public static void demoMethod() {
      System.out.println("method of super class");
   }
}
public class Sample extends Demo {
   public static void demoMethod() {
      System.out.println("method of sub class");
   }
   public static void main(String args[] ) {
      Sample.demoMethod();
   }
}

Output

method of sub class

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements