Inheritance in Java
Let’s say that record shop we discussed above also sells Blu-ray movies.
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:
- public instance methods.
- 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.
A class can extend only one class however it can implement any number of interfaces. An interface can extend more than one interfaces.
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.
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");
}
}
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");
}
}
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.
We can implement this diagram in Java to avoid code duplication.
Advantages of inheritance
- Code reuse: child class inherits all instance members of parent class.
- More flexible to change code: changing code in place is enough.
- 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!