Skip to main content
Documents
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming

Java Inheritance

Java Inheritance is a fundamental concept in object-oriented programming that allows a new class to inherit properties and behaviors (fields and methods) from an existing class. This promotes code reusability and establishes a hierarchical relationship between classes.

Key Concepts

  • Superclass (Parent Class): The class whose properties and methods are inherited.
  • Subclass (Child Class): The class that inherits from the superclass.

Usage

Inheritance is used to create a new class that is a type of an existing class. It helps in extending the functionality of a class without modifying it, thereby adhering to the Open/Closed Principle of software design.

Syntax


class Superclass {
    // fields and methods
}

class Subclass extends Superclass {
    // additional fields and methods
}

Examples

Example 1: Basic Inheritance


class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

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

public class InheritanceExample {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();  // Inherited method
        dog.bark(); // Subclass method
    }
}

In this example, Dog is a subclass that inherits the eat method from the Animal superclass. The Dog class also has its own method, bark.

Example 2: Overriding Methods


class Vehicle {
    void run() {
        System.out.println("The vehicle is running.");
    }
}

class Car extends Vehicle {
    @Override
    void run() {
        System.out.println("The car is running safely.");
    }
}

public class MethodOverridingExample {
    public static void main(String[] args) {
        Car car = new Car();
        car.run(); // Overridden method
    }
}

Here, the Car class overrides the run method of the Vehicle class to provide a specific implementation.

Example 3: Using super Keyword


class Person {
    String name = "John";

    void display() {
        System.out.println("Person's name: " + name);
    }
}

class Employee extends Person {
    String name = "Doe";

    void display() {
        System.out.println("Employee's name: " + name);
        super.display(); // Call superclass method
    }
}

public class SuperKeywordExample {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.display();
    }
}

In this example, the super keyword is used to call the display method of the superclass Person from the subclass Employee.

Tips and Best Practices

  • Use Inheritance Judiciously: Only use inheritance when there is a true "is-a" relationship. For example, a Dog is an Animal.
  • Avoid Deep Inheritance Hierarchies: Keep inheritance hierarchies shallow to maintain simplicity and reduce complexity.
  • Prefer Composition Over Inheritance: Consider using composition (has-a relationship) instead of inheritance when appropriate, as it offers more flexibility.
  • Use @Override Annotation: Always use the @Override annotation when overriding methods to improve code readability and prevent errors.
  • Access Control: Use the protected access modifier for superclass members that should be accessible in subclasses but not to the outside world.

Learn Java Essentials

Build your Java skills from the ground up and master programming concepts.
Start Learning Java for Free