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

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 08-Jan-2018 08:32:18
Advertisements