简体   繁体   English

是否可以创建从另一个子类扩展的子类?

[英]Is it possible to create a subclass which extends from another subclass?

I am still learning how to code java and had a question about how inheritance works. 我仍在学习如何编写Java代码,并对继承的工作方式有疑问。 Class A is the parent class and Class B is the subclass which inherits all the methods from Class A. Suppose I create a third class, Class C. If I do Class C extends Class B, is this different than doing Class C extends Class A? 类A是父类,类B是从类A继承所有方法的子类。假设我创建了第三个类C,如果我对类C进行了类B扩展,则与对类C进行类A扩展有所不同? If so, how? 如果是这样,怎么办? Thank You. 谢谢。 (Sorry if the format sucked) (对不起,如果格式很烂)

The simplest way to visualize this is to consider that inheritance is like a parent/child relationship. 可视化的最简单方法是考虑继承就像父/子关系。 You can have Parent -> Child -> Grand Child, etc. 您可以有“父母”->“孩子”->“孙子”等。

When you have: 当你有:

class A {}
class B extends A{}
class C extends B{}

C is like a grand child of A . C像是A的孙子。 And that means C inherits all the methods from B , including those methods that B itself inherited from A. In OOP words, C **is** A`. 这意味着C将从B继承所有方法,包括B本身从A. In OOP words,继承的那些方法A. In OOP words, C **is** A`。

However, when you have 但是,当你有

class A {}
class B extends A{}
class C extends A{}

C and B are sibling classes, meaning they both inherit A 's methods, but they are incompatible with one another. CB同级类,这意味着它们都继承A的方法,但是彼此不兼容。


In the first case, these are valid: 在第一种情况下,这些是有效的:

C c = new C();
c.methodFromA(); //C inherits methods from A by being its grand-child
c.methodFromB(); //C inherits methods from B by being its child
c.methodFromC();

In the second case, however, when both B and C extends A directly: 但是,在第二种情况下,当BC都直接extends A

C c = new C();
B b = new B();
c.methodFromA(); //C inherits methods from A by being its child
b.methodFromA(); //B inherits methods from A by being its child

c.methodFromB(); //not allowed
b.methodFromC(); //not allowed

However, there's no direct relationship between B and C . 但是, BC之间没有直接关系。 These are invalid: 这些是无效的:

B b = new B();
C c = new C();

b = (B) c; //invalid, won't compile

A b = b;
c = (C) b; //will compile, but cause a ClassCastException at runtime

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个子类对象创建子类对象 - Create subclass object from another subclass object 如何创建一个子类的 object,它扩展了另一个 class,它位于另一个 class 内部? - How to create an object of a subclass, that extends another class, which is inside yet another class? Java创建Iterable <?在没有警告的情况下从Iterator <SubClass>扩展SuperClass> - Java create Iterable<? extends SuperClass> from Iterator<SubClass> without warning 是否可以将最初作为子类对象创建的对象更改为另一个子类对象? - Is it possible to change an object which was originally created as a subclass object to another subclass object? Java 子类包含来自另一个子类的 object - Java subclass containing object from another subclass 从超类创建一个子类 - Create a subclass from superclass 将子类转换为另一个子类 - cast subclass to another subclass JLS 的哪一部分指定您不能从 List 投射<!--? extends List<Superclass--> &gt; 列出<list<subclass> &gt;? </list<subclass> - Which part of the JLS specifies that you can't cast from List<? extends List<Superclass>> to List<List<Subclass>>? 从超类调用一个子类方法,该方法在另一个子类中不可用 - Call a subclass method from superclass which is not available in another sub class 扩展超类的子类如何调用父类的生成器 - How a subclass which extends a superclass calls the builder of parent class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM