Java extends Keyword: Learn Class Inheritance with Examples

0

In the previous chapter, you learned that inheritance allows one class to derive from another. But how does Java know which class should inherit from which? The answer is the extends keyword.

The extends keyword creates an inheritance relationship between a child class and a parent class. It tells Java that the child class should inherit the accessible members of the specified parent class.

extends is used when one Java class inherits from another class.

Why Do We Need extends?

Suppose an application already has a Vehicle class containing common behavior. When you create a Car class, you may want the car to automatically have that vehicle behavior instead of implementing it again.

class Vehicle {
    void start() {
        System.out.println("Vehicle starts");
    }
}

class Car extends Vehicle {
    void drive() {
        System.out.println("Car is driving");
    }
}

The statement class Car extends Vehicle means that Car is a subclass of Vehicle. The Car class can therefore use accessible members inherited from Vehicle.

Basic Syntax

class ChildClass extends ParentClass {
    // Child class members
}

The class before extends is the child class, while the class after extends is the parent class.

ChildClass extends ParentClass → ChildClass inherits from ParentClass.

A Complete Example

class Employee {
    String name = "Rahul";

    void work() {
        System.out.println("Employee is working");
    }
}

class Developer extends Employee {
    void writeCode() {
        System.out.println("Developer is writing code");
    }
}

public class Main {
    public static void main(String[] args) {
        Developer developer = new Developer();

        System.out.println(developer.name);
        developer.work();
        developer.writeCode();
    }
}

The Developer class does not declare name or work(). They come from Employee. The developer object can also call its own writeCode() method.

What Does extends Establish?

The extends keyword establishes a parent-child relationship between classes. This relationship is important because it affects inheritance, method overriding, polymorphism, constructor execution, and access to inherited members.

Part Example Meaning
Child class Developer The class that inherits.
extends extends Keyword that establishes class inheritance.
Parent class Employee The class being inherited from.

Can a Class Extend Multiple Classes?

No. Java does not allow a class to extend more than one class directly. This means the following code is invalid:

// Invalid Java code

class A {
}

class B {
}

class C extends A, B {
}

Java deliberately avoids multiple class inheritance because it can create ambiguity when two parent classes provide conflicting implementations. Java instead provides interfaces for supporting multiple inheritance of type.

A Java class can directly extend only one class, but a class can implement multiple interfaces.

extends and the Object Class

There is an important detail that often surprises beginners: every Java class ultimately inherits from Object, directly or indirectly.

class Animal {
}

class Dog extends Animal {
}

Conceptually, the inheritance chain is Dog → Animal → Object. Even though Animal does not explicitly extend Object, Java makes Object its implicit superclass.

One Parent, Many Children

The same parent class can be extended by multiple child classes. For example, both Car and Bike can extend Vehicle.

class Vehicle {
    void start() {
        System.out.println("Vehicle starts");
    }
}

class Car extends Vehicle {
}

class Bike extends Vehicle {
}

Both child classes inherit the accessible start() method. This pattern is the foundation of hierarchical inheritance, which you will explore in a later chapter.

extends Does Not Mean Everything Becomes Accessible

The extends keyword creates inheritance, but access modifiers still apply. For example, a private member declared in the parent class cannot be accessed directly from the child class.

class Parent {
    private int value = 10;
}

class Child extends Parent {
    void display() {
        // System.out.println(value); // Compile-time error
    }
}

The parent still owns the private field. The child simply cannot access it directly. This distinction becomes especially important when designing encapsulated classes.

Common Beginner Mistakes

  • Trying to extend two classes using extends.
  • Assuming every parent member can be accessed directly from the child.
  • Using inheritance when the relationship is not a genuine is-a relationship.
  • Confusing extends with implements.
  • Forgetting that every Java class ultimately derives from Object.

extends vs implements

Keyword Used With Purpose
extends Class Creates inheritance between a child class and a parent class.
implements Interface Allows a class to provide implementations for an interface's contract.

Interview Insight

A strong interview answer is: "The extends keyword is used in Java to establish inheritance between classes. A class can directly extend only one class, and the child class can inherit accessible members from its parent while adding or overriding behavior."

Final Takeaway

The extends keyword is the doorway through which class inheritance is established in Java. Once you see class Car extends Vehicle, you should immediately recognize that Car is a specialized form of Vehicle. Understanding this relationship prepares you for the next step: learning how inheritance works when one child class extends another, which leads naturally to single inheritance.

Post a Comment

0Comments
Post a Comment (0)