简体   繁体   English

java中的继承(子类的子类)

[英]Inheritance in java(subclass of a subclass)

say we have three classes: class a , class b , class c;假设我们有三个班级:a 班,b 班,c 班; class b inherits class a , if we define that class c inherits class b(which inherits class a) will the code give an error .If not the can we say that class c inherits class a;类b继承类a,如果我们定义类c继承类b(继承类a)代码会报错。如果不是,我们可以说类c继承类a吗?

in all i ask that can there be a subclass of a subclass??总而言之,我问可以有一个子类的子类吗??

In short, yes , you could definitely have a "chain" of inheritance.简而言之,是的,您绝对可以拥有继承的“链”。 When you have a class A that inherits another class B, then it doesn't matter whether class B inherits from another class.当你有一个类 A 继承了另一个类 B 时,那么类 B 是否继承自另一个类并不重要。

Though, you should keep in mind that a class is not able to inherit from multiple classes (it would throw a compiler error).但是,您应该记住,一个类不能从多个类继承(它会引发编译器错误)。 Multiple inheritance in Java is achievable through the use of interfaces. Java 中的多重继承可以通过使用接口来实现。

Yes, Multilevel inheritance refers to a mechanism where one can inherit from a derived class, thereby making this derived class the base class for the new class.是的,多级继承是指一种可以从派生类继承的机制,从而使该派生类成为新类的基类。

for example例如

Class A
{
   public void methodA()
   {
     System.out.println("Class A method");
   }
}
Class B extends A
{
public void methodB()
{
System.out.println("class B method");
}
}
Class C extends B
{
   public void methodC()
   {
     System.out.println("class C method");
   }
   public static void main(String args[])
   {
     C obj = new C();
     obj.methodA(); //calling grand parent class method
     obj.methodB(); //calling parent class method
     obj.methodC(); //calling local method
  }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM