Inheritance in Java

Introduction to Inheritance in Java

Inheritance in Java is a mechanism in object-oriented programming (OOP) that allows you to create new classes based on existing classes. The existing class is known as the **parent class** or **superclass**, and the new class is known as the **child class** or **subclass**.

Inheritance allows you to reuse the code and behavior of the parent class in the child class. This can save you time and effort when writing your code, and it can also make your code more readable and maintainable.

Types of inheritance in Java

Java supports the following four types of inheritance:

  • Single inheritance: This is the most common type of inheritance, where a child class can inherit from only one parent class.
  • Multilevel inheritance: This type of inheritance allows a child class to inherit from a parent class that has already inherited from another parent class.
  • Hierarchical inheritance: This type of inheritance allows multiple child classes to inherit from the same parent class.
  • Multiple inheritance: This type of inheritance allows a child class to inherit from multiple parent classes. However, multiple inheritance is not supported directly in Java. It can be achieved indirectly using interfaces.

Benefits of using inheritance in Java

There are several benefits to using inheritance in Java, including:

  • Code reuse: Inheritance allows you to reuse the code and behavior of the parent class in the child class. This can save you time and effort when writing your code.
  • Readability and maintainability: Inheritance can make your code more readable and maintainable by making it clear what code is inherited from the parent class and what code is new in the child class.
  • Polymorphism: Inheritance allows you to implement polymorphism, which is the ability of objects of different types to respond to the same method in different ways.

Example of inheritance in Java

The following example shows how to use single inheritance in Java:

// Parent class
class Animal {
  public void eat() {
    System.out.println("The animal is eating.");
  }
}

// Child class
class Dog extends Animal {
  @Override
  public void eat() {
    System.out.println("The dog is eating.");
  }
}

// Usage
public class Main {
  public static void main(String[] args) {
    Dog dog = new Dog();
    dog.eat(); // Prints "The dog is eating."
  }
}

In this example, the `Dog` class inherits from the `Animal` class. This means that the `Dog` class has access to all of the public methods and fields of the `Animal` class. The `Dog` class also overrides the `eat()` method to provide its own implementation.

Inheritance is a powerful feature of Java that can help you write more robust and efficient code. If you are working with Java, I encourage you to learn more about inheritance and how to use it in your code.

Exercises

Code demonstrating inheritance relationship between classes. Person class is inherited by Student and Faculty class in Java


  package javaprogrammingdemo;
  public class javalabclass{
      public static void main(String args[]) 
      {
        //creating object of student class
        student ram = new student("ram","pune","12322","12bce");
        ram.login("test", "test");
        ram.display_student();
        //creating an object of faculty class
        faculty satish = new faculty("satish","vellore","23333","111","564");
        satish.login("test", "test");
        satish.display_faculty();
      }
  }
  
  //super class person
  class person
  {
    String name;
    String address;
    String phonenumber;
    
    public person(String name, String address, String phonenumber) {
      this.name = name;
      this.address = address;
      this.phonenumber = phonenumber;
    }	
    
    public void login(String username, String password)
    {
      if(username.equals("test") && password.equals("test"))
      {
        System.out.println("Valid User");
      }
      else
      {
        System.out.println("Check your username and password");
      }
    }
  }
  
  //student class inherits person class 
  class student extends person
  {
    String regno;
  
    public student(String name, String address, String phonenumber, String regno) {
      super(name, address, phonenumber);
      this.regno = regno;
    }
    
    public void display_student()
    {
      System.out.println(this.name+this.address+this.phonenumber+this.regno);
    }
  }
  
  //faculty class inherits person class
  class faculty extends person
  {
    String empid;
    String cabinno;
    public faculty(String name, String address, String phonenumber, String empid, String cabinno) {
      super(name, address, phonenumber);
      this.empid = empid;
      this.cabinno = cabinno;
    }
  
    public void display_faculty()
    {
      System.out.println(this.name+this.address+this.phonenumber+this.empid+this.cabinno);
    }
  }