Inheritance in Java

Thanoshan MV
4 min readMay 1, 2020

Let’s say that record shop we discussed above also sells Blu-ray movies.

Figure 1: Movie, StockKeeper class diagram

As you can see the above diagram, there are many common states and behaviors (common logic) between Album and Movie.

When implementing this class diagram into code, are you going to write (or copy & paste) the entire code for Movie? If you do, you are repeating yourself. How to avoid code duplication?

This is where we use inheritance.

Inheritance is a mechanism in which one object acquires all the instance members of its parent.

Inheritance uses parent-child relationship (IS-A relationship).

So what exactly is inherited?

Only instance members are inherited.

NOTE: No inheritance for static and private members.

Visibility/access modifiers impact what gets inherited from one class to another.

In Java, as a rule of thumb we make instance variables private and instance methods public .

In this case, we can safely say that the following are inherited:

  1. public instance methods.
  2. private instance variables (private instance variables can be accessed only through public getter and setter methods).

Types of Inheritance in Java

There are five types of inheritance in Java. They are single, multilevel, hierarchical, multiple, and hybrid.

Class allows single, multilevel and hierarchical inheritances. Interface allows multiple and hybrid inheritances.

Figure 2: Java inheritance types

A class can extend only one class however it can implement any number of interfaces. An interface can extend more than one interfaces.

Figure 3: Explains inheritance keywords.

Relationships

I. IS-A relationship

IS-A relationship refers the inheritance or implementation.

a. Generalization

Generalization uses a IS-A relationship from a specialization class to generalization class.

Figure 4: Generalization diagram

II. HAS-A relationship

Instance of one class HAS-A reference to an instance of another class.

a. Aggregation

In this relationship, the existence of class A and B are not dependent on each other.

For this aggregation part, we going to see an example of Student class and ContactInfo class.

class ContactInfo {
private String homeAddress;
private String emailAddress;
private int telephoneNumber; //12025550156
}
public class Student {
private String name;
private int age;
private int grade;
ContactInfo contactInfo;//Student HAS-A ContactInfo
public void study() {
System.out.println("Study");
}
}
Figure 5: Class diagram shows the generalization relationship

Student HAS-A ContactInfo. ContactInfo can be used in other places – for example, a company's Employee class can also use this ContactInfo class. So Student can exist without ContactInfo and ContactInfo can exist without Student . This type of relationship is known as aggregation.

b. Composition

In this relationship, class B can not exist without class A — but class A can exist without class B.

To give you an idea about composition, let’s see an example of Student class and StudentId class.

class StudentId {
private String idNumber;//A-123456789
private String bloodGroup;
private String accountNumber;
}
public class Student {
private String name;
private int age;
private int grade;
StudentId studentId;//Student HAS-A StudentId
public void study() {
System.out.println("Study");
}
}
Figure 6: Class diagram shows the composition relationship

Student HAS-A StudentId. Student can exist without StudentId but StudentId can not exist without Student. This type of relationship is known as composition.

Now, let’s back to our previous record shop example that we discussed above.

Figure 7: Class diagram with inheritance

We can implement this diagram in Java to avoid code duplication.

Advantages of inheritance

  1. Code reuse: child class inherits all instance members of parent class.
  2. More flexible to change code: changing code in place is enough.
  3. To use polymorphism: method overriding requires IS-A relationship.

Now we know about inheritance. If you have any doubts or if you think any other important concepts inside the inheritance is missed here please feel free to let me know. So that we can improve our article and help others to learn easily.

Thank you for reading.

I’ll meet you on the Abstraction in Java article.

Happy coding!

--

--