在Java中继承是编译时还是运行时? [英] Is inheritance compile time or runtime in java
本文介绍了在Java中继承是编译时还是运行时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人告诉我继承是运行时,但是我想问一下,如果继承是运行时,那么当我们尝试通过子类对象访问父类数据成员时,编译器在编译时如何产生错误:
I was told that inheritance is runtime, but I want to ask that if inheritance is runtime then how does compiler generate error at compile time when we try to access parent class data member via child class object:
class PrivateData {
private int x = 50;
void show() {
System.out.println(x);
}
}
class ChildPrivateData extends PrivateData {
public static void main(String s[]) {
ChildPrivateData c1 = new ChildPrivateData();
System.out.println(c1.x);
}
}
推荐答案
继承绝对是在Java编译时定义的.我认为您将其与多态性混淆了,简而言之,它指出Java选择仅在运行时运行的重写方法.
Inheritance is most definitely defined at compile time in Java. I think you're confusing it with polymorphism, which, in a nutshell, states that Java chooses which overridden method to run only at runtime.
这篇关于在Java中继承是编译时还是运行时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文