Understanding inheritance
There are three core pillars in Java: polymorphism, inheritance, and encapsulation (data hiding). It is easy to remember them using the acronym “PIE” (Polymorphism, Inheritance, and Encapsulation). Let us now examine inheritance.
Inheritance is a code reusability mechanism where common properties between related types are exploited by forming relationships between those types. Inheritance relationships in Java are created by extending from a class or by implementing an interface. We will cover interfaces in Chapter 10, so for the moment, we will assume classes throughout. To understand why inheritance in OOP is important, we will examine its advantages (and disadvantages). As we have not covered the terminology used yet, this discussion will be somewhat abstract.
Advantages of inheritance
One principle advantage of inheritance is code reuse. A new class can be written based on an existing class rather than writing the new class from...