DEV Community

A Ramesh
A Ramesh

Posted on

Java Inheritance

1. GeeksforGeeks – Inheritance in Java

  • Covers the basics of inheritance with syntax and examples.
  • Explains method overriding, runtime polymorphism, and code reusability.
  • Includes a neat example with Animal, Dog, Cat, and Cow classes.
class Animal {
    void sound() {
        System.out.println("Animal makes a sound");
    }
}

class Dog extends Animal {
    void sound() {
        System.out.println("Dog barks");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog d = new Dog();
        d.sound();  // Output: Dog barks
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Great Learning – Inheritance in Java with Examples

  • Breaks down single, multilevel, and hierarchical inheritance.
  • Uses simple code snippets to show how child classes inherit from parent classes.
  • Great for beginners who want to understand the “extends” keyword and class relationships.

Multilevel Inheritance Example:

class A {
    void displayA() {
        System.out.println("Class A");
    }
}

class B extends A {
    void displayB() {
        System.out.println("Class B");
    }
}

class C extends B {
    void displayC() {
        System.out.println("Class C");
    }
}

public class Main {
    public static void main(String[] args) {
        C obj = new C();
        obj.displayA();
        obj.displayB();
        obj.displayC();
    }
}
Enter fullscreen mode Exit fullscreen mode

3. Java2Blog – Core Java Inheritance

  • Explains inheritance as an IS-A relationship.
  • Discusses why Java doesn’t support multiple inheritance directly.
  • Includes a practical example with variables and method access.
class Vehicle {
    int speed = 60;
    void move() {
        System.out.println("Vehicle is moving");
    }
}

class Car extends Vehicle {
    void displaySpeed() {
        System.out.println("Speed: " + speed);
    }
}

public class Main {
    public static void main(String[] args) {
        Car c = new Car();
        c.move();              // Output: Vehicle is moving
        c.displaySpeed();      // Output: Speed: 60
    }
}
Enter fullscreen mode Exit fullscreen mode

Additional Explanation

What is Inheritance in Java?

Inheritance is a mechanism in Java where one class acquires the properties and behaviors of another class. It allows for code reusability and establishes a relationship between parent and child classes.


Types of Inheritance

  • Single Inheritance: A child class inherits from a single parent class.
class Parent {
    void greet() {
        System.out.println("Hello from Parent");
    }
}

class Child extends Parent {
    void message() {
        System.out.println("Hello from Child");
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Multilevel Inheritance:
    (Refer to the example under Great Learning section.)

  • Hierarchical Inheritance:

class Shape {
    void draw() {
        System.out.println("Drawing a shape");
    }
}

class Circle extends Shape {
    void circleInfo() {
        System.out.println("This is a circle");
    }
}

class Square extends Shape {
    void squareInfo() {
        System.out.println("This is a square");
    }
}
Enter fullscreen mode Exit fullscreen mode

Why Java Doesn’t Support Multiple Inheritance

Java avoids multiple inheritance to prevent ambiguity caused by the diamond problem. Instead, it uses interfaces to achieve similar functionality.


Key Benefits of Inheritance

  • Code Reusability: Reduces redundancy by reusing existing code.
  • Polymorphism: Enables dynamic method invocation.
  • Extensibility: Makes it easier to add new features.

Top comments (0)