Inheritance in Java

    Atul Kabra3 min readUpdated

    Inheritance lets a new class build on an existing one, reusing its fields and methods and adding or changing behaviour. The new class is the subclass (or child); the one it extends is the superclass (or parent). You declare it with extends. Inheritance models an "is-a" relationship — a Manager is an Employee — and removes duplication by sharing common code in the parent.

    The extends keyword

    class Employee {
        String name;
        double salary;
    
        void describe() {
            System.out.println(name + " earns " + salary);
        }
    }
    
    // Manager IS-A Employee: it inherits name, salary, and describe().
    class Manager extends Employee {
        int teamSize; // adds its own field
    }
    

    A Manager object can use name, salary, and describe() without redeclaring them, plus its own teamSize.

    Method overriding

    A subclass can replace an inherited method with its own version — this is overriding. Mark it with @Override so the compiler verifies you really are overriding something.

    class Employee {
        void describe() {
            System.out.println("An employee.");
        }
    }
    class Manager extends Employee {
        @Override
        void describe() {                      // replaces the parent's version
            System.out.println("A manager.");
        }
    }
    // A Manager's describe() prints "A manager."
    

    When you call describe() on a Manager, Java runs the manager's version. This runtime selection is the heart of polymorphism.

    The super keyword

    super refers to the parent. Use it to call the parent's method or constructor from the child.

    class Manager extends Employee {
        @Override
        void describe() {
            super.describe();                  // run the parent's version first
            System.out.println("...who manages a team."); // then add to it
        }
    }
    

    This lets you extend parent behaviour rather than fully replacing it.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    Constructor chaining

    A subclass constructor always runs the parent constructor first, so the inherited part is initialised before the child's. If you do not call super(...) explicitly, Java inserts a call to the parent's no-arg constructor.

    class Employee {
        String name;
        Employee(String name) {       // parent has no no-arg constructor
            this.name = name;
        }
    }
    class Manager extends Employee {
        int teamSize;
        Manager(String name, int teamSize) {
            super(name);              // MUST call parent constructor first
            this.teamSize = teamSize;
        }
    }
    

    Because Employee has only a parameterized constructor, the child must call super(name) explicitly — otherwise the code will not compile.

    The single-inheritance rule

    A Java class can extend one class only. This avoids the ambiguity of multiple inheritance. To share behaviour from more than one source, you implement multiple interfaces (covered in the interfaces tutorial). Every class ultimately inherits from java.lang.Object, the root of all classes.

    Common mistakes

    • Forgetting super(...) when the parent lacks a no-arg constructor. The compiler then cannot insert the implicit call and reports an error.
    • Misspelling an overridden method. Without @Override, a typo creates a new method instead of overriding — @Override catches this immediately.
    • Overusing inheritance. If the relationship is "has-a", not "is-a", prefer composition (hold the other object as a field).
    • Trying to extend two classes. Java forbids it; use interfaces instead.
    • Reducing visibility when overriding. An overriding method cannot be more restrictive than the one it overrides (e.g. you cannot override a public method as private).

    FAQ

    What is the difference between overloading and overriding? Overloading is same name, different parameters, in one class (compile-time). Overriding is a subclass redefining a parent method with the same signature (runtime).

    Can a subclass access the parent's private fields? No. Private members are not inherited for direct access; use the parent's public/protected methods instead.

    Keep going

    Next, see overriding in action with Polymorphism in Java, or learn shared contracts in Interfaces & Abstract Classes. All tutorials are on the Java hub; for guided practice in Jalgaon, see the Java course.


    Design reusable class hierarchies at Infoplanet, Jalgaon. Join the waitlist for the Java course.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides