What is Inheritance in Java
What you will learn about inheritance in java here:
- What is inheritance in java
- Why we use inheritance in java
- Important points about inheritance in java
- Structure of inheritance in java
- Types of inheritance in java
- Single level inheritance in java
- Multi level inheritance in java
- Hierarchical inheritance in java
- Multiple level inheritance in java
What is inheritance in java?
Inheritance in java is a process of acquiring the properties i.e states and behavior of one class in another class. In simple words, we can say one class accessing the states and behavior of another class is nothing but the inheritance. In Java, we achieve inheritance using the keyword extends. extends is represented by the Solid arrow.
Why we use inheritance in java?
1)When multiple classes have the same properties i.e states and behavior, then writing the same properties again and again in each class increases the code redundancy. Code redundancy means writing the unnecessary code again and again.
Code redundancy is avoided by using inheritance in java which is shown below. Common properties are written in one class which is called a parent class or base class. classes that are using the common properties are called a child class or derived class. In the below example, the Human class is parent class and Doctor, Engineer, Teacher, Worker are child classes.
2)We can achieve code reusability by using inheritance in java.
3)We can generalization by using inheritance in java.
4)We can achieve polymorphism and abstraction by using inheritance in java.
Important points about inheritance in java
Below important points about inheritance in java are given.
1)We can only inherit variables and methods of class which is nothing but the states and behavior of class respectively.
2)Inheritance is implemented by using a keyword called extends.
3)We can’t inherit Constructor of class.
4)we can’t inherit private methods and variables of a class.
5)When multiple classes implementing the same or common properties i.e variables and methods then, we write those same or common properties in one class to avoid the code redundancy.
6)The class which has common or same properties is called a super class or parent class or base class.
7) The class which acquiring or extending the properties from the parent class is called a sub class or childclass or derived class.
8)Only one class can extends another one class at a time.
9)When the compilation of code happens, every class in java automatically extends super most class called Object class.
10)If a class is extending another class then it doesn’t extend object class directly but still, it indirectly extends Object class through multi-level inheritance.
Structure of inheritance
The general structure of inheritance in java is shown below.
Types of Inheritance
Below types of inheritance in java are listed and explained with the help of example.
1)Single level inheritance
2)Multi level inheritance
4)Multiple level inheritance
Single Level Inheritance:
One class acquiring or extending another class is nothing but the Single level inheritance. In the below example, the Human class is a parent class or base class which has common properties and the Engineer class is a child class which is acquiring the properties of the Human class (parent class). Engineer class acquiring the properties from parent class using keyword called extends followed by parent class name which is shown below.
Multi Level Inheritance:
One class extending another class and again another class extending the subclass is called as multi level inheritance.
When more than one classes acquire the properties of one class, then such type of inheritance is called a Hierarchical Inheritance.
Multiple Level Inheritance:
When one class acquires the properties of more than one class then such type of inheritance is called as multiple level inheritance. But this is not possible to implement using a class as one class can acquire the properties of one class at a time. Therefore Multiple Level Inheritance is implemented by using Interface. Please click on the below link to know, how implement multiple level inheritance in java using Interface.
Hybrid inheritance is a combination of Hierarchical Inheritance and Multiple Level Inheritance. It is not possible to implement using class. It is possible to implement using Interface.