<aside> <img src="/icons/table_red.svg" alt="/icons/table_red.svg" width="40px" /> Table of Contents
</aside>
<aside> 💡
Inheritance in Java
Inheritance is the most powerful feature of object-oriented programming. It allows us to inherit the properties of one class into another class.
Inheritance
Inheritance is a mechanism of driving a new class from an existing class. The existing (old) class is known as base class or super class or parent class. The new class is known as a derived class or sub class or child class. It allows us to use the properties and behavior of one class (parent) in another class (child).
A class whose properties are inherited is known as parent class and a class that inherits the properties of the parent class is known as child class. Thus, it establishes a relationship between parent and child class that is known as parent-child or Is-a relationship.
Suppose, there are two classes named Father and Child and we want to inherit the properties of the Father class in the Child class. We can achieve this by using the extends keyword.
//inherits the properties of the Father class
class Child extends Father {
// functionality
}
When we should use inheritance?
Points to Remember
Types of Inheritance
Single Inheritance
Multi-level Inheritance
Hierarchical Inheritance
Hybrid Inheritance.
Single Inheritance
In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes it is also known as simple inheritance.
In the above figure, Employee is a parent class and Executive is a child class. The Executive class inherits all the properties of the Employee class.
Implementation of the single inheritance mechanism in a Java program.
class Employee {
float salary = 34534 * 12;
}
public class Executive extends Employee {
float bonus = 3000 * 6;
public static void main(String args[]) {
Executive obj = new Executive();
System.out.println("Total salary credited: " + obj.salary);
System.out.println("Bonus of six months: " + obj.bonus);
}
}
Output:
Total salary credited: 414408.0
Bonus of six months: 18000.0
Multi-level Inheritance
Multi-level inheritance occurs when a class is derived from another class, and that derived class further inherits from another class. In simpler terms, it's like a chain of inheritance where one class inherits from another, and that inherited class inherits from yet another class.
In the above figure, the class Marks inherits the members or methods of the class Students. The class Sports inherits the members of the class Marks. Therefore, the Student class is the parent class of the class Marks and the class Marks is the parent of the class Sports. Hence, the class Sports implicitly inherits the properties of the Student along with the class Marks.
Implementation of the multi-level inheritance mechanism in a Java program.
//super class
class Student {
int reg_no;
void getNo(int no) {
reg_no = no;
}
void putNo() {
System.out.println("registration number= " + reg_no);
}
}
// intermediate sub class
class Marks extends Student {
float marks;
void getMarks(float m) {
marks = m;
}
void putMarks() {
System.out.println("marks= " + marks);
}
}
// derived class
class Sports extends Marks {
float score;
void getScore(float scr) {
score = scr;
}
void putScore() {
System.out.println("score= " + score);
}
}
public class MultilevelInheritanceExample {
public static void main(String args[]) {
Sports ob = new Sports();
ob.getNo(0987);
ob.putNo();
ob.getMarks(78);
ob.putMarks();
ob.getScore(68.7);
ob.putScore();
}
}
Output:
registration number = 0987
marks= 78.0
score= 68.7