Method Hiding in Java: Definition and Usage



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: 2019-07-30T22:30:20+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements