One of the really useful features of Object-Oriented programming is inheritance. You may have heard of someone coming into an inheritance, which often means they were left something from a relative who died. Or, you might hear someone say that they have inherited musical ability from a parent. In Java all classes can inherit attributes (instance variables) and behaviors (methods) from another class. The class being inherited from is called the parent class or superclass. The class that is inheriting is called the child class or subclass.
When one class inherits from another, we can say that it is the same kind of thing as the parent class (the class it inherits from). For example, a car is a kind of vehicle. This is sometimes called the is-a relationship, but more accurately it’s a is-a kind of relationship. A motorcycle is another kind of vehicle. All vehicles have a make, model, and year that they were created. All vehicles can go forward, backward, turn left and turn right.
It’s simple enough to say, yes I get it, a car is a kind of Vehicle and a motorcycle is a kind of Vehicle also. Cars and motorcycles can both turn and both have a make, model and year. BUT, how do we do this in code and why would it even be useful?
Turn to your code. Create the following classes under Lab 3:
Arduino
Book
CISItem
Magazine
Phone
ReadingMaterial
Take a second to think which should be the Parent Classes and which classes should inherit from the parent?
Which of the classes is generic enough to hold information for other classes?
Can a Magazine generalize the information needed to create a Phone?
Can the term CISItem generalize the information needed to create a Book?
To make a subclass inherit from a superclass, use the Java keyword extends with the superclass name when creating a new subclass as shown below. While a real-life person can have two parents, a Java class can only inherit from one parent class.
public class Car extends Vehicle public class Motorcycle extends Vehicle
We can use the extends keyword to create the above inheritance structure within our lab. In this case, CISItem defines information that all Items in the system will have. For example, all phones, arduinos, books and magazines should have a name, location, price and description. However, if we look at the Book class UML diagram, we notice that there is no location variable. This is because it would inherit it from its parent class. BUT, its parent class ReadingItem does not contain a location variable either. This means that our code would look like this:
public class ReadingItem extends CISItem public class Book extends ReadingItem
Book inherits from ReadingItem and ReadingItem inherits from CISItem.
As we progress through this unit we will find ways in which inheritance will help us create programs with a robust design. We won’t have to rewrite as much code because Inheritance allows you to reuse data and behavior from the parent class. If you notice that several classes share the same data and/or behavior, you can pull that out into a parent class. This is called generalization. For example, Books and Magazines are both reading items so it makes sense use the general ReadingItem class as seen in the UML diagram above.
Inheritance is also useful for specialization which is when you want most of the behavior of a parent class, but want to do at least one thing differently and/or add more data. The example above, in the UML, can also be seen as specialization. A magazine has many of the same attributes as a Book like wordCount and date published. However, a Magazine will most likely have a cover story, where a book will have a title. This specialized data is exactly why we need two separate classes for a Book and a Magazine.