Inheritance enables a class to obtain all the properties from another class and works in an IS-A relationship manner. It empowers code reusability and minimize duplication of code. Further, a class can be the superclass of multiple classes. All classes have a superclass except the Object class. Object class is the root class of all the classes in Java.
In Java, there are various types of inheritance in which single inheritance is the simplest type to understand.
Single inheritance can be defined as a type of inheritance, where a single parent class is inherited by only a single child class. The class which inherits another class, is termed a derived class or subclass or child class, whereas the class from which it’s extended, is termed as the superclass or parent class or base class.
The below diagram represents that a class named B extends only a single class which is A. In this, A is the superclass and B is the subclass.
In other words, single inheritance is a type of inheritance that consists of only a single child and single parent class. It follows a basic is-a relationship. A child class inherits attributes and behaviour from its superclass. It is important to know that a class can only inherit public, default and protected members from its parent class.
class A { // members } class B extends A { // members }
As we know, the inheritance concept focuses on the program’s modularity and code reusability. Through single inheritance, we’ll be demonstrating the same.
Program:
public class A { public void display() { System.out.println("I am a method from class A"); } } // B is inheriting display method of A class B extends A { public void print() { System.out.println("I am a method from class B"); } public static void main(String[] args) { B objB = new B(); objB.display(); // Reusing the method of A named display objB.print(); } }
Output:
I am a method from class A I am a method from class B
Explanation :
In the above example, class A has a method named display() and class B is another class which is having a method named print(). Further, class B is using “extends” keyword to inherit the properties of A. Therefore, class A is the parent class and class B is the child class. Lastly, we’ve created the object of class B, and are successfully able to access the display() method of A.
Without inheritance, we need to write a display method with the same code in class B also to achieve the functionality. It would lead to code duplication, make it lengthy and increase the complexity.
Example 2. Program to calculate area of rectangle using single inheritance in Java
In this example, we’ll be calculating area of a rectangle by inheriting, an area calculation method of a base class.
Program:
class Shape { // calculateArea is member of shape class public int calculateArea(int length,int breadth) { return length * breadth; } } // Inheriting Shape member using extends keyword class Rectangle extends Shape { public static void main(String[] args) { Rectangle rectangle = new Rectangle(); // creating object of child class System.out.println("Area of rectangle :: "+ rectangle.calculateArea(10,5)); // calcualateArea method accessible to rectangle class } }
Output:
Area of rectangle :: 50
Explanation:
In the above example, we have two classes namely Shape and Rectangle where Rectangle is inheriting Shape class. The shape class has a method named calculateArea() which is being directly used by the rectangle class to get the area.
In this example, we’ll be calculating salary of a programmer. A programmer can be an employee, so, there’s a IS-A relationship. The same’s been implemented below.
Program:
class Employee { float salary; Employee(){ this.salary = 50000; } } class Programmer extends Employee{ float bonus; Programmer(){ this.bonus = 2000; } public static void main(String[] args) { Programmer programmer = new Programmer(); float totalSalary = programmer.getTotalSalary(programmer.salary, programmer.bonus); System.out.println("Total salary for you programmer :: "+totalSalary); } float getTotalSalary(float basicSalary, float bonus){ return basicSalary + bonus; } }
Output:
Total salary for you programmer :: 52000.0
Explanation:
In the above example, the Employee class has the salary field which can be viewed as a basic salary and the Programmer class is also having a field bonus.The Programmer class is extending the Employee class. It can use the base salary of the Employee class to calculate the total salary by adding bonus value.
In this example, we’ll be using a super keyword with an inheritance concept. super() is used to refer instance of the superclass. In other words, super(id) will invoke the constructor of the parent class that accepts id as a parameter. Below is the code.
Program:
class Library { int id; Library(int id) { this.id = id; } } class Student extends Library { String name; Student(int id, String name) { super(id); this.name = name; } public static void main(String[] args) { Student student = new Student(1, "Pintu"); System.out.println("Id = " + student.id + " Student Name = " + student.name); } }
Output:
Id = 1 Student Name = Pintu
Explanation:
In the above code, we have Library and Student class where Student is inheriting Library class. The library has a field namely id which is being used to capture student records by mapping the student’s name.
This post was last modified on September 3, 2022