O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Getting ready

Inheritance is the ability of one class to extend (and, optionally, override) the properties and/or 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 as a type for the references to the objects of its subclasses.

To demonstrate the power of these two concepts, let's create classes that represent cars and trucks, each having weight, engine power, and speed it can reach (as a function of time) with 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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required