--> What Is Inheritance In Java? » The Problem Solution

What is Inheritance in Java?

When Inheritance is the topic then code reusability is the king of that topic. Inheritance builds a base-child relationship where we not only access the features of the child class but also we can access the features of the parent class.

inheritance
  • Inheritance: It means one class inherits the fields (properties) and methods (behaviors) of another class.
  • Parent Class (Superclass): The class whose properties and methods are inherited.
  • Child Class (Subclass): The class that inherits from the parent class.
  • extends: It is a keyword, used to define a child class from a parent class or child interface to a parent interface
  • implements: This keyword helps to define a class to interface.
  1. Single Inheritance: A class has only one child class.
  2. Multilevel Inheritance: A class inherits from a parent class, which itself inherits from a grandparent class, creating a multilevel inheritance hierarchy.
  3. Hierarchical Inheritance: Multiple classes inherit from a single parent class, forming a hierarchy where the child classes share the characteristics and behaviors of the parent class.
  4. Multiple Inheritance: Not directly supported with classes, but interfaces can be achieved.

In single inheritance, a class inherits from one parent class. This is the easiest form of inheritance.

// Base class
class Cricket {
    void meaning() {
        System.out.println("This is a outdoor game.");
    }
}

// Child Class
class Virat extends Cricket {
    void speciality() {
        System.out.println("In this game, I am a world class batsmen.");
    }
}

public class Main {
    public static void main(String[] args) {
        Virat vk = new Virat ();
        vk.meaning();  // This is a outdoor game.
        vk.speciality(); // I am a world class batsmen in this game.
    }
}
Java

In multilevel inheritance, a class inherits from a parent class, which in turn inherits from another parent class, forming a chain.

// Base Class
class Cricket {
    void meaning() {
        System.out.println("This is a outdoor game.");
    }
}

// 1st Child Class
class Sachin extends Cricket {
    void speciality() {
        System.out.println("I am the GOD of this game.");
    }
}

// 2nd Child Class
class Arjun extends Sachin {
    void quality() {
        System.out.println("Still searching, Once I found, I'll let you know.");
    }
}

public class Main {
    public static void main(String[] args) {
        Arjun at = new Arjun();
        at.meaning();  // This is a outdoor game.
        at.speciality(); // I am the GOD of this game.
        at.quality(); // Still searching, Once I found, I'll let you know.
    }
}
Java

In hierarchical inheritance, multiple classes inherit from a single-parent class.

// Base class
class Cricket {
    void meaning() {
        System.out.println("This is a outdoor game, very famous in the world.");
    }
}

// Child Class 1
class Dhoni extends Cricket {
    void speciality() {
        System.out.println("I am an Indian wicket-keeper batsmen.");
    }
}

// Child Class 2
class Lee extends Cricket {
    void speciality() {
        System.out.println("I am an Australian right arm fast bowler.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dhoni msd = new Dhoni();
        Lee bl = new Lee();
        
        msd.meaning();  // This is a outdoor game, very famous in the world.
        msd.speciality(); // I am an Indian wicket-keeper batsmen.
        
        bl.meaning();  // This is a outdoor game, very famous in the world.
        bl.speciality(); // I am an Australian right arm fast bowler.
    }
}
Java

Java does not support multiple inheritance with classes (a class cannot inherit from more than one class directly). However, this can be achieved using interfaces, which allow a class to implement multiple interfaces.

interface Cricket {
    void famousFor();
}

class Virat implements Cricket {
    
    @Override
    public void famousFor() {
        System.out.println("Cover drive.");
    }
}

class Dhoni implements Cricket {
    
    @Override
    public void famousFor() {
        System.out.println("Wicket keeping and helicopter shot.");
    }
}

public class Main {
    public static void main(String[] args) {
        Virat vk = new Virat();
        vk.famousFor(); // Cover drive.
        
        Dhoni msd = new Dhoni();
        msd.famousFor();// wicket keeping and helicopter shot.
    }
}
Java
  • Method Overriding: A child class can provide its specific implementation of a method that is already defined in its parent class.
  • Super Keyword: Used to refer to the immediate parent class object and can be used to access parent class methods and constructors.
  • this keyword: is used to refer to the current class instance variable and also access the current class methods and constructors.

Inheritance in Java allows the creation of new classes based on existing classes. This promotes code reusability and establishes a clear hierarchical relationship between classes, making the code easier to manage and understand.

Leave a Reply

Your email address will not be published. Required fields are marked *