The purpose of inheritance in software development is to facilitate the reuse of safe and reliable software. One of the major benefits of using inheritance is that it eliminates redundant code in your programs.
How Inheritance Works
The idea behind inheritance is that many classes or objects have some of the same set of attributes and methods. Therefore, in the spirit of producing reliable software, new classes can now draw from pre-existing related classes and if need be expand on existing states and behaviors.
A real-world example of how inheritance works would be to consider fruits. This is a broad label that serves to encapsulate a range of different items.
An apple is a fruit and so is an orange. However, an orange is not an apple, so you wouldn’t have fruits as one of your stock items if you owned a store. Perhaps you could have a fruits section in your inventory, and under that section, you would have more specific items like apples and oranges.
That is how inheritance works.
Using Inheritance in Java
Inheritance can be used in any programming language that uses the object-oriented programming paradigm. However, the exact way in which inheritance is used is dependent on the specific programming language.
For example, C++ is also an object-oriented programming language. C++ supports what is known as multiple inheritance, while Java only supports single inheritance.
What this means is that in Java a parent class can have many child classes, but each child class can only have a single parent class (single inheritance). However, there is a way to achieve indirect multiple inheritance in Java, by creating a grandparent, parent, and child relationship.
Creating the Parent Class in Java
The process of selecting a parent class from a document of software requirements is known as object-oriented analysis. During this process the phrase “is a” is often used to identify possible inheritance relationships. Drawing from our example above you should be able to see that fruit would be our parent class.
Fruit Parent Class Example
One of the most notable aspects of the parent class above is the access modifier that is used with each variable declaration. The “protected” access modifier is ideal for use in parent classes because it prevents non-child classes from gaining access to the data attributes of the parent class.
Further down in the code you are introduced to constructors, getters, and setters that are general building blocks for any Java class. Finally, you are introduced to two methods (juice and eat) that are created in the parent class of our program because they are universal to all fruits—all fruits can be eaten and juiced.
Creating Child Classes in Java
Child classes are usually called specialized or derived classes because they inherit state and behavior from a parent, and often customize these attributes to be more specific.
Continuing with our example, you should be able to see why orange would be a suitable child class of the fruit class above.
Orange Child Class Example
There is a difference between what a regular Java class declaration looks like, and what we have in our code above. The “extends” keyword is what is used in Java to make inheritance possible.
In our example above the child class (orange) extends the parent class (fruit). Therefore, the state and behavior of the fruit class can now be accessed and modified by the orange class.
The unique attribute that our orange class has is identified with the variable name supremes (which is the official name for the little segments found in oranges). This is where specialization comes into play; not all fruits have supremes but all oranges do, so reserving the supremes variable for the orange class is logical.
Adding the “peel” method to the pre-existing “eat” and “juice” methods is also logical because though not all fruits can be peeled, oranges often are peeled.
You should bear in mind that if we did not intend to alter the existing “eat” and “juice” methods, we would not need to include them in our orange class. The methods in the orange class override any similar method in the fruit class. So if all fruits were eaten and juiced in the same way, we would not need to create these methods in the orange class.
The Role Constructors Play in Inheritance
By default, parent class constructors are inherited by child classes. Therefore, if a child class object is created this means that a parent class object is also created automatically.
Going back to our example, each time a new orange object is created a fruit object is also created because an orange is a fruit.
Behind the scenes, when a child class object is created, the constructor of the parent class is called first followed by the constructor of the child class. In our orange child class above, if an orange object is created without any parameters our default fruit class constructor will be called, followed by our default orange class contractor.
The “super” method in our primary constructor above is necessary because it specifies that the primary constructor—and not the default constructor—of the parent fruit class should be called whenever an orange object with parameters is created.
Now You Can Use Inheritance in Java
From this article, you were able to learn what inheritance is, how it works, and why it is such an important concept in programming. You can now create your inheritance relationships using the Java programming language. Furthermore, you now know how to get around Java’s single inheritance rule by creating a grandparent relationship.
Image Credit: Andreas Wohlfahrt/Pexels