๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Inheritance in Java  


Introduction
  • Inheritance means acquiring the properties and behaviors of a parent class in a child class.
  • It allows a subclass (child class) to inherit fields and methods from a superclass (parent class), promoting code reuse and method overriding.
  • Inheritance represents an IS-A relationship, also known as a parent-child relationship. It signifies that a subclass is a type of its superclass.
  • For example:
    • A Car IS-A Vehicle.
    • A Dog IS-A Animal.
    • A Surgeon IS-A Doctor.
  • How to achieve inheritance in Java?
    • By using the extends keyword for class inheritance.
    • By using the implements keyword for interface inheritance.
  • Program 1 (using extends keyword): :
    class Vehicle
    {
        void start()
        {
            System.out.println("Vehicle starts.");
        }
    }
    
    class Car extends Vehicle
    {
        void drive()
        {
            System.out.println("Car drives.");
        }
    }
    
    public class MainApp
    {
        public static void main(String[] args)
        {
            Car myCar = new Car();
    
            myCar.start(); // inherited from Vehicle
            myCar.drive(); // specific to Car
        }
    }
  • Program 2 (using implements keyword): :
    interface Animal
    {
        void eat();
    }
    class Dog implements Animal
    {
        public void eat()
        {
            System.out.println("Dog eats.");
        }
    }
    public class MainApp
    {
        public static void main(String[] args)
        {
            Dog myDog = new Dog();
            myDog.eat(); // inherited from Animal
    
            //Animal myAnimal = new Animal();   // error because we cannot create an object of an interface
        }
    }

Advantages of Inheritance
  • Code Reusability: Inheritance allows a child class to reuse the code of its parent class.
  • Easy Maintenance: Changes made in the parent class automatically propagate to child classes, making maintenance easier.
  • Method Overriding: Inheritance enables method overriding, allowing a child class to provide a specific implementation of a method already defined in its parent class.
  • Polymorphism: Inheritance supports runtime polymorphism using method overriding.
Disadvantages of Inheritance
  • Tight Coupling: Inheritance creates a tight coupling between parent and child classes, if we change the parent class, it may affect all child classes.
  • Increased Complexity: Inheritance can lead to complex class hierarchies, making the code harder to understand and maintain.

Types of Inheritance
  • There are 5 types of inheritance in Java:
    1. Single Inheritance: One class inherits the properties and behaviors of one parent class.
    2. Multilevel Inheritance: One class inherits the properties and behaviors of a parent class, and that class is inherited by another class.
    3. Hierarchical Inheritance: Multiple classes inherit the properties and behaviors of a single parent class.
    4. Multiple Inheritance: One class inherits the properties and behaviors of multiple classes. (Not supported in Java directly, but can be achieved using interfaces.)
    5. Hybrid Inheritance: A combination of two or more types of inheritance. (Not supported in Java directly, but can be achieved using interfaces.)
  • Click Here to learn more about the types of inheritance in Java.

Important Points
  • Some important points to remember about inheritance in Java:
    • Java does not support multiple and hybrid inheritance with classes to avoid ambiguity, such as the diamond problem.
    • A class can extend only one class, which is known as single inheritance.
    • Constructors and private members of the parent class are not inherited by the child class.
    • A class can implement multiple interfaces, which is Java's way of achieving multiple inheritance.
    • The super keyword is used to refer to the parent class, such as accessing parent class methods or constructors.
    • The this keyword is used to refer to the current class instance, commonly used to differentiate between instance variables and parameters.