Thursday, 9 February 2017

JAVA: Method Hiding In Java:



Method Hiding In Java:


We cannot ovverride static methods but we can overload the static methods.
If both Parent and Child classes methods are static then we won't get compile time error.It seems to be Overriding bt it is not overriding,it is method hiding.
Note1:we can't override static method as non-static method.
Note 2:we can't override non-static method as static method
Example:
package com.rameshsoft.rameshselenium;

public class MethodHiding extends Parent1{
          public static void method()
          {
                   System.out.println("Child class method()");
          }
          public static void main(String[] args) {
                   Parent1 parent1=new Parent1();
                   parent1.method();
                   Parent1 parent2=new MethodHiding();
               parent2.method();//parent method will be called                 
                   MethodHiding child=new MethodHiding();
                   child.method();;                  
          }
}
class Parent1
{
          public static void method()
          {
                   System.out.println("Parent class method()");
          } 
}
OUTPUT:
Parent class method()
Parent class method()
Child class method()

No comments:

Post a Comment