JAVA OOPS - Inheritance
We will learn about inheritance and its importance along with its different types.
Welcome to 4th article of Java OOPs series, in this we will learn everything about Inheritance with examples.
What is Inheritance?
Inheritance is one of the important pillars of OOPS. It is a mechanism of inheriting/acquiring all properties and behaviors (variables and methods) of a class by another class.
The class from which we are inheriting properties is called the Parent/super class.
The class which inherits parent class properties is called child/subclass.
Inheritance represents the IS-A relationship which is also known as a parent-child relationship.
class Parent{
void m1(){
System.out.println("Parent Class");
}
}
class Child extends Parent{
void m2(){
System.out.println("Child Class");
}
}
Whenever we are inheriting a Parent class in Java, we need to use extends keyword.
In the above example, the Parent class has a method m1().
Child class has a method m2().
But since the child class inherits parent class, m1()(from parent class) is also available to the child class.
What if no Inheritance?
Let's suppose we need to create a loan application. There are 3 types of loans
-> Home Loan
-> Personal Loan
-> Education Loan
class HomeLoan{
//30 methods
}
class PersonalLoan{
//30 methods
}
class EducationLoan{
//30 methods
}
We have created 3 different classes for each loan.
If each loan requires implementing 30 different methods.
In total, we have implemented 90 methods.
But, when we see all the 3 classes are of type Loan, there must be some common methods between them.
Let's suppose there are 20 common methods among the 3 classes, there will be a lot of code duplicacy.
So to avoid that let's create a class Loan and implement the 20 common methods.
class Loan{
//20 methods implemented
}
class HomeLoan extends Loan{
//remaining 10 methods specific to HomeLoan is implemented
// rest 20 from the Loan(Parent) class
}
class PersonalLoan extends Loan{
//remaining 10 methods specific to PersonalLoan is implemented
// rest 20 from the Loan(Parent) class
}
class EducationLoan extends Loan{
//remaining 10 methods specific to EducationLoan is implemented
// rest 20 from the Loan(Parent) class
}
Then we only need to inherit the Loan class and implement 10 methods specific to each loan.
In this way, with the help of inheritance, we can reuse a lot of code.
Why use Inheritance?
- Code Reusability.
- Method Overriding (we will see this in polymorphism)
Types of Inheritance
a) Single Inheritance
It is the simplest type. When one class extends another(only one) class, that type is called single inheritance.
Here, B is inheriting A. Class A is the parent class and Class B is the child class.
class A{
void m1(){
System.out.println("Parent Class A");
}
}
class B extends A{
void m2(){
System.out.println("Child Class B");
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.m1();
b.m2();
}
}
/*
OUTPUT
Parent Class A
Child Class B
*/
b) Multilevel Inheritance
Multilevel inheritance refers to the chain of inheritance.
As in the above image, C is the child class of B and B is the child class of A.
Here, Class C will have all the methods and variables of A and B as well.
class A{
void m1(){
System.out.println("Parent Class A");
}
}
class B extends A{
void m2(){
System.out.println("Child Class B - Parent A");
}
}
class C extends B{
void m3(){
System.out.println("Child Class C - Parent B");
}
}
public class Main {
public static void main(String[] args) {
C c = new C();
c.m1();
c.m2();
c.m3();
}
}
/*
OUTPUT
Parent Class A
Child Class B - Parent A
Child Class C - Parent B
*/
c) Hierarchical Inheritance
In this type, one class has multiple sub/child classes.
In the above example, B, C, and D are child classes of the same Parent A.
class A{
void m1(){
System.out.println("Parent Class A");
}
}
class B extends A{
void m2(){
System.out.println("Child Class B - Parent A");
}
}
class C extends A{
void m3(){
System.out.println("Child Class C - Parent A");
}
}
class D extends A{
void m4(){
System.out.println("Child Class D - Parent A");
}
}
public class Main {
public static void main(String[] args) {
B b = new B();
b.m1();
b.m2();
}
}
/*
OUTPUT
Parent Class A
Child Class B - Parent A
*/
d) Multiple Inheritance
When one class extends(or inherits) multiple classes. This type is called Multiple inheritance.
Till now we have seen different types of inheritances all of them had only one base or parent class but the problem with this is there are multiple parent classes.
Due to this java doesn't support Multiple inheritance.
Let us understand why?
class A{
void m1(){
System.out.println("Parent Class A");
}
}
class B {
void m1(){
System.out.println("Parent Class B");
}
}
class C extends A,B{ // suppose it is valid
void m2(){
System.out.println("Child Class C - Parent A");
}
}
public class Main {
public static void main(String[] args) {
C c = new C();
c.m1(); // which m1() method to execute? from A or B
}
}
Here, let's suppose C inherits A and B where both A and B have a method m1() with the same signature.
So when the object of C is created and c.m1() is called, which m1() method will be executed?
This is an ambiguity due to which java doesn't support multiple inheritance for classes.
But we can make use of multiple inheritance in terms of interfaces because in interfaces as the methods are abstract, even though 2 parent interfaces have the same abstract method their implementation will always be done once.
Conclusion
That is it for this article.
We learned about inheritance, why the inheritance is important and different types of inheritance.
From the next article onwards we will start with Polymorphism.
If you find this helpful, do like it.
Any suggestions comment down below.