Inheritance 继承
发布于 2021 年 10 月 29 日更新于 2021 年 11 月 2 日
Inheritance is an object-oriented programming (OOP) concept by which the properties and behaviors from a parent class are passed on to a child class.
继承是面向对象编程(OOP)中的一个概念,通过它,父类的属性和行为被传递给子类。
In day-to-day life, when something gets passed from a parent to a child, it is said that child inherits from their parent. Similarly, in Java, when some properties or behaviors get passed from parent class to the child class, it is said that child class inherits from a parent class.
Child classes can have properties and methods of its own as well.
They can also override the behaviors (methods) of the parent class. This is called polymorphism (another OOP concept).
We can implement inheritance by using the extends
keyword.
Syntax
Here’s the basic syntax for implementing inheritance:
public class Child extends Parent {// Class body}
Example
public class Vehicle {private int numWheels;private String engineNum;private String maker;private String color;public int getNumWheels() {return numWheels;}public void setNumWheels(int wheels) {this.numWheels = wheels;}public String getEngineNum() {return engineNum;}public void setEngineNum(String engineNum) {this.engineNum = engineNum;}// Similar getters and setters for other properties}
In the code above, Vehicle
is a class that has properties and behaviors. This is more of a generic class.
While creating a specific class, for example, Car
which has these properties and behaviors and some extra properties, instead of defining all these properties again in the new class, we can make use of inheritance to pass on the properties defined in Vehicle
class and access them in Car
class. This would make code written in Vehicle
class reusable.
Similarly, we can make another class, for example, Airplane
, which can access the properties present in Vehicle
class.
同样地,我们可以创建另一个类,例如Airplane
,它可以访问Vehicle
类中的属性。
Here’s a Car
class which inherits Vehicle
class:
这是一个继承Vehicle
类的Car
类:
public class Car extends Vehicle {// Vehicle class properties and methods accessible hereprivate int airbagCount;public int getAirbagCount() {return airbagCount;}public void setAirbagCount(int airbagCount) {this.airbagCount = airbagCount;}}
Below is how you can access properties and methods of parent class:
以下是如何访问父类的属性和方法:
public static void main(String args[]) {Car car = new Car();car.setNumWheels(4); // Parent class method. This would set the wheel count to 4System.out.println(car.getNumWheels()); // Parent class method. Prints numWheelscar.setAirbagCount(2); // Child class method. This would set the airbag countSystem.out.println(car.getAirbagCount()); // Child class method. Prints airbag count}
Types of Inheritance 继承的类型
Single Inheritance 单一继承
Single inheritance is when a child class inherits from a parent class.
单一继承是指子类从父类继承。
public class Parent {// Parent class properties and methods}public class Child extends Parent {// Parent class properties and methods accessible here due to inheritance// Child class properties and methods}
Multilevel Inheritance 多级继承
Multilevel inheritance is when a child class inherits from a parent class which in turn inherits from another parent class.
多级继承是指子类继承自一个父类,而这个父类又继承自另一个父类。
public class Parent {// Parent class properties and methods}public class Child extends Parent {// Parent class properties and methods accessible here due to inheritance// Child class properties and methods}public class GrandChild extends Child {// Parent class and Child class properties and methods accessible here due to inheritance// GrandChild class properties and methods}
Hierarchical Inheritance 层次继承
Hierarchical inheritance is when a child class inherits from a parent class and there is another sibling class that also inherits from the same parent class.
层次继承是指一个子类继承自父类,同时还有另一个子类也继承自同一个父类。
public class Parent {// Parent class properties and methods}public class Child extends Parent{// Parent class properties and methods accessible here due to inheritance// Child class properties and methods}public class Sibling extends Parent{// Parent class properties and methods accessible here due to inheritance// Child class properties and methods are not accessible here// Sibling class properties and methods}
Advantages of Inheritance
继承的优势
- Code reusability: Same properties and methods of a class can be used by inheriting that class.
代码重用性:通过继承,一个类的属性和方法可以被多个子类使用。 - Lower maintenance cost: If a piece of code needs to be updated, it can be done at minimal place if inheritance is used.
维护成本低:如果需要更新代码,使用继承可以在最少的地方进行修改。 - Easier to add new features: If a same new feature needs to be added to multiple classes, through inheritance it can be added in parent class and all the child classes inheriting this parent class would get that feature instantly.
更易于添加新功能:如果需要向多个类添加相同的新功能,通过继承可以在父类中添加该功能,所有继承此父类的子类将立即获得该功能。
All contributors 所有贡献者
- Anonymous contributor
- Anonymous contributor
- Anonymous contributor
- Anonymous contributor
Looking to contribute? 想要贡献吗?
- Learn more about how to get involved.
了解更多关于如何参与的信息。 - Edit this page on GitHub to fix an error or make an improvement.
在 GitHub 上编辑此页面以修复错误或进行改进。 - Submit feedback to let us know how we can improve Docs.
提交反馈让我们知道我们如何改进文档。