What is method hiding in Java? Even the JavaDoc explanation is confusing

前端 未结 7 1300
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 02:36

Javadoc says:

the version of the hidden method that gets invoked is the one in the superclass, and the version of the overridden method that gets invo

7条回答
  •  囚心锁ツ
    2020-11-28 02:56

     class P
        {
        public static  void m1()
        {
        System.out.println("Parent");
        }
    }
        class C extends P
        {
        public static void m1()
        {
        System.out.println("Child");
        }
    }
    class Test{
        public static void main(String args[])
        {
        Parent p=new Parent();//Parent
        Child c=new Child();  //Child
        Parent p=new Child();  //Parent
        }
        }
    
    If the both parent and child class method are static the compiler is responsible for method resolution based on reference type
    
    class Parent
    {
    public void m1()
    {
    System.out.println("Parent");
    }}
    class Child extends Parent
    {
    public void m1()
    {
    System.out.println("Child")
    }
    }
    class Test
    {
    public static void main(String args[])
    {
    Parent p=new Parent(); //Parent 
    Child c=new Child();   //Child
    Parent p=new Child();  //Child
    }
    }
    
    If both method are not static  jvm is responsible for method resolution based on run time object
    

提交回复
热议问题