Overriding private methods and visibility

守給你的承諾、 提交于 2019-12-25 17:44:07

问题


I know that private methods are hidden in Derived class and they cant be overridden but my question is different.. Please go through the following problem. My question is mentioned in the problem stated below:

TestPolymorphism.java

class CheckParent {
    private void display() {
        System.out.println("This is parent class");
    }
}

class CheckChild extends CheckParent {
    void display() {
        System.out.println("This is child class");
    }
}

public class TestPolymorphism  {
    public static void main(String[] args) {
        CheckParent cp = new CheckChild();
        cp.display();        // This will throw error as display() method id private
        //  and invisible to child class
    }

However, in following code snippet no exception is thrown.

CheckParent.java

  public class CheckParent {
       private void display() {
            System.out.println("This is parent class");
        }
        public static void main(String[] args) {
            CheckParent cp = new CheckChild();

            cp.display();   /*This will print "This is parent class" without any error
            * my question is why no error is thrown like above case
            * as here too display() method is private and invisible to
            * derived class */
        }
    }

    class CheckChild extends CheckParent {
        void display() {
            System.out.println("This is child class");

        }
    }
}

回答1:


For constructor, private, final or static methods static(early) binding is used. For all others dynamic(late) binding is used. Check this out:

http://geekexplains.blogspot.co.uk/2008/06/dynamic-binding-vs-static-binding-in.html




回答2:


If you would like to override a parent class method please consider placing the @Override annotation in front of the overriding method, so that compiler can warn you of possible mistakes.

I do not think you are overriding the display() method of a parent class with display() method of a child class, because the parent class's method is private. You are rather defining an new method.

class CheckParent {
    //remove private access modifier to have the method overriden in child class
    private void display() {
        System.out.println("This is parent class");
    }
}

class CheckChild extends CheckParent {
    @Override //IDE or compiler will warn you that you are not overriding anything        
    void display() {
        System.out.println("This is child class");
    }
}

Please see Bloch's Effective Java. 2nd Edition, Item 36.

In the second code snippet you create CheckChild instance but store is as CheckParent accessing the private void display() of CheckParent instance method inside the CheckParent class's static method. Class members (static method in your case) can access it's private methods and fields, so this code is valid.

If you want to access the instance void display() method of the CheckChild class instance in the second code snippet you should cast the CheckParent instance to CheckChild first.

public class CheckParent {
    private void display() {
        System.out.println("This is parent class");
    }
    public static void main(String[] args) {
        CheckParent cp = new CheckChild();
        cp.display();   //"This is parent class"

        if (cp instanceof CheckChild){
            ((CheckChild) cp).display(); //"This is child class"
        }
     }
}
class CheckChild extends CheckParent {
    void display() {
        System.out.println("This is child class");
    }
}

Anyway such coding approach looks like a misuse of inheritance to me.



来源:https://stackoverflow.com/questions/25061713/overriding-private-methods-and-visibility

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!