Java 重写私有方法和可见性

Java 重写私有方法和可见性,java,overriding,private-methods,Java,Overriding,Private Methods,我知道私有方法隐藏在派生类中,它们不能被重写,但我的问题不同。。请检查下面的问题。下面的问题中提到了我的问题: testpolymorphics.java class CheckParent { private void display() { System.out.println("This is parent class"); } } class CheckChild extends CheckParent { void display() {

我知道私有方法隐藏在派生类中,它们不能被重写,但我的问题不同。。请检查下面的问题。下面的问题中提到了我的问题:

testpolymorphics.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
    }
但是,在下面的代码段中,不会引发异常

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");

        }
    }
}

对于构造函数,使用私有、最终或静态方法静态(早期)绑定。对于所有其他情况,使用动态(后期)绑定。看看这个:


< p>如果您想重写父类方法,请考虑在重写方法前面放置<代码> @覆盖> <代码>注释,以便编译器可以警告您可能出现的错误。

我不认为您正在用子类的
display()
方法覆盖父类的
display()
方法,因为父类的方法是
private
。你在定义一种新方法

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");
    }
}
请参阅Bloch的
effectiveJava。第二版,第36项

在第二个代码段中,您创建了CheckChild实例,但存储为CheckParent,访问CheckParent类的
static
方法中CheckParent实例方法的
private void display()
。类成员(在您的示例中是静态方法)可以访问它的私有方法和字段,因此此代码是有效的

如果要访问第二个代码段中CheckChild类实例的实例
void display()
方法,应首先将CheckParent实例强制转换为CheckChild

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");
    }
}

无论如何,在我看来,这种编码方法似乎是对继承的滥用。

在回答任何问题之前,你能告诉我你正在创建多少个文件吗?我的意思是说,您是将所有内容都写在一个.java文件中,还是它们不同?因为这也会使方法的范围有所不同。嘿…我把它分为第一个程序和第二个程序,这使它更清楚了。。。。。我的问题是,由于单词PRIVATE的可见性问题引发了错误,这是可以理解的,但是为什么在第二个程序中没有抛出错误,尽管由于单词PRIVATE仍然存在可见性问题,因为我正在对子对象进行父类型引用..是的..没有进行重写..嘿..我已经通过将其分类为第一个程序和第二个程序来更清楚地说明。。。。。我的问题是,抛出错误是因为word PRIVATE导致的可见性问题,这是可以理解的,但为什么在第二个程序中没有抛出错误,尽管由于word PRIVATE仍然存在可见性问题,因为我正在对子对象进行父类型引用。。