Open In App

What is the Need of Inheritance in Java?

Improve
Improve
Like Article
Like
Save
Share
Report

Inheritance, as we have all heard is one of the most important features of Object-Oriented Programming Languages whether it is Java, C++, or any other OOP language. But what is the need for Inheritance? Why is it so an important concept

Inheritance can be defined as a mechanism by which one object can acquire all the properties (i.e. data members) and behavior (i.e. member functions or methods) of a parent object.

The basic idea of Inheritance is to create the new class (called child class or derived or subclass) from an existing class (called parent class or Base or Superclass). That is, the child class inherits the properties (methods and fields) of the parent class.

Thinking to achieve something without Inheritance will be the misuse of time and resources as we have to write the same code again and again.

Let us take an example, three classes Person, Employee and Student have some attributes, some common and some different. See the code below.

Java




// Java program without inheritance
import java.io.*;
import java.math.BigDecimal;
  
// class person
class Person {
  
    private String name;
    private int age;
  
    public String getName() { return name; }
  
    public void setName(String name) { this.name = name; }
  
    public int getAge() { return age; }
  
    public void setAge(int age) { this.age = age; }
  
    public String toString()
    {
        return String.format(
            "Person :- name : %s , age : %d", name, age);
    }
}
  
//class student
class Student {
  
    private String name;
    private int age;
    private int rollno;
    private String school;
  
    public String getName() { return name; }
  
    public void setName(String name) { this.name = name; }
  
    public int getAge() { return age; }
  
    public void setAge(int age) { this.age = age; }
  
    public int getRollno() { return rollno; }
  
    public void setRollno(int rollno)
    {
        this.rollno = rollno;
    }
  
    public String getSchool() { return school; }
  
    public void setSchool(String school)
    {
        this.school = school;
    }
  
    public String toString()
    {
        return String.format(
            "Student :- name : %s , age : %d , roll : %d , school : %s",
            name, age, rollno, school);
    }
}
  
//class Employee
class Employee {
  
    private String name;
    private int age;
    private double salary;
    private String organisation;
  
    public String getName() { return name; }
  
    public void setName(String name) { this.name = name; }
  
    public int getAge() { return age; }
  
    public void setAge(int age) { this.age = age; }
  
    public double getSalary() { return salary; }
  
    public void setSalary(double salary)
    {
        this.salary = salary;
    }
  
    public String getOrganisation() { return organisation; }
  
    public void setOrganisation(String organisation)
    {
        this.organisation = organisation;
    }
  
    public String toString()
    {
        return String.format(
            "Employee :- name : %s , age : %d , organisation : %s , salary : %f",
            name, age, organisation, salary);
    }
}
  
// class PersonRunner
public class PersonRunner {
  
    public static void main(String[] args)
    {
        Person person = new Person();
        Student student = new Student();
        Employee employee = new Employee();
  
        person.setName("Saurabh");
        person.setAge(20);
  
        student.setName("Prateek");
        student.setAge(21);
        student.setRollno(101);
        student.setSchool("New Era HS");
  
        employee.setName("Sushant");
        employee.setAge(25);
        employee.setOrganisation("GeeksforGeeks");
        employee.setSalary(50000.00);
  
        System.out.println(person);
        System.out.println(student);
        System.out.println(employee);
    }
}


Output

Person :- name : Saurabh , age : 20
Student :- name : Prateek , age : 21 , roll : 101 , school : New Era HS
Employee :- name : Sushant , age : 25 , organisation : GeeksforGeeks , salary : 50000.000000

Note that we have to write the same code which is already written in Person class again in Student and in Employee class. This takes time and memory. 

Now read the code below carefully where we are using Inheritance.

Java




// Java program with inheritance
import java.io.*;
  
class Person {
  
    private String name;
    private int age;
  
    public String getName() { return name; }
  
    public void setName(String name) { this.name = name; }
  
    public int getAge() { return age; }
  
    public void setAge(int age) { this.age = age; }
  
    public String toString()
    {
        return String.format(
            "Person :- name : %s , age : %d", getName(),
            getAge());
    }
}
  
// Student class is inheriting the properties of Person
// class( through extend keyword) Therefore, we don't need to
// declare name and age (and their related methods which are
// covered in Person) again.
class Student extends Person {
  
    private int rollno;
    private String school;
  
    public int getRollno() { return rollno; }
  
    public void setRollno(int rollno)
    {
        this.rollno = rollno;
    }
  
    public String getSchool() { return school; }
  
    public void setSchool(String school)
    {
        this.school = school;
    }
  
    public String toString()
    {
        return String.format(
            "Student :- name : %s , age : %d , roll : %d , school : %s",
            getName(), getAge(), getRollno(), getSchool());
    }
}
  
// Employee class is inheriting the properties of Person
// class( through extend keyword) Therefore, we don't need to
// declare name and age (and their related methods which are
// covered in Person) again.
class Employee extends Person {
  
    private double salary;
    private String organisation;
  
    public double getSalary() { return salary; }
  
    public void setSalary(double salary)
    {
        this.salary = salary;
    }
  
    public String getOrganisation() { return organisation; }
  
    public void setOrganisation(String organisation)
    {
        this.organisation = organisation;
    }
  
    public String toString()
    {
        return String.format(
            "Employee :- name : %s , age : %d , organisation : %s , salary : %f",
            getName(), getAge(), getOrganisation(),
            getSalary());
    }
}
  
public class PersonRunner {
  
    public static void main(String[] args)
    {
        Person person = new Person();
        Student student = new Student();
        Employee employee = new Employee();
  
        person.setName("Saurabh");
        person.setAge(20);
  
        student.setName("Prateek");
        student.setAge(21);
        student.setRollno(101);
        student.setSchool("New Era HS");
  
        employee.setName("Sushant");
        employee.setAge(25);
        employee.setOrganisation("GeeksforGeeks");
        employee.setSalary(50000.00);
  
        System.out.println(person);
        System.out.println(student);
        System.out.println(employee);
    }
}


Output

Person :- name : Saurabh , age : 20
Student :- name : Prateek , age : 21 , roll : 101 , school : New Era HS
Employee :- name : Sushant , age : 25 , organisation : GeeksforGeeks , salary : 50000.000000

Note that we have written less code in the above example when Inheritance is used compare to the first example where Inheritance was not used and thus we had to define the name, age, and their related methods again and again.

That is, we have utilized the concept of reusability as we have reused the code written in Person class over and over again.

Also, the above example shows Method Overriding (as toString function of Person class is overridden in both the Student and the Employee class, which is also a major feature of Inheritance.

Thus, it concludes that the main aim of inheritance is to implement the concept of reusability, saving our time and resources and also creating better connections between different classes, and achieve method overriding. 



Last Updated : 17 Mar, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads