
上QQ阅读APP看书,第一时间看更新
Getting ready
Inheritance is the ability of one class to get ownership of the non-private fields and methods of another class.
The extended class is called the base class, superclass, or parent class. The new extension of the class is called a subclass or child class.
Polymorphism is the ability to use the base class type for the reference to an object of its subclass.
To demonstrate the power of inheritance and polymorphism, let's create classes that represent cars and trucks, each with the weight, engine power, and speed it can reach (as a function of time) with the maximum load. In addition, a car, in this case, will be characterized by the number of passengers, while a truck's important feature will be its payload.