Skip to main content
Documents
Share
LinkedIn
Facebook
Twitter
Copy
Java keywords

extends Keyword in Java

The extends keyword in Java is used to indicate that a class is inheriting from a superclass. This is a fundamental aspect of object-oriented programming in Java, allowing a new class to inherit fields and methods from an existing class.

Usage

The extends keyword is used in class declarations to establish an inheritance relationship between two classes.

Syntax

class SubclassName extends SuperclassName {
    // class body
}
  • SubclassName: The name of the new class that is inheriting.
  • SuperclassName: The name of the existing class being inherited from.

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 Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.eat();  // Inherited method
        dog.bark(); // Method of Dog class
    }
}

In this example, the Dog class extends the Animal class, inheriting its eat method. The Dog class also has its own method bark. When an instance of Dog is created, both the inherited eat method and the bark method can be called.

Example 2: Overriding Methods

class Animal {
    void sound() {
        System.out.println("This animal makes a sound.");
    }
}

class Cat extends Animal {
    @Override
    void sound() {
        System.out.println("The cat meows.");
    }
}

public class Main {
    public static void main(String[] args) {
        Cat cat = new Cat();
        cat.sound(); // Overridden method
    }
}

Here, the Cat class extends the Animal class and overrides the sound method to provide its own implementation. The @Override annotation is used to indicate that the method is being overridden.

Example 3: Inheriting Fields

class Person {
    String name;
}

class Employee extends Person {
    int employeeId;
}

public class Main {
    public static void main(String[] args) {
        Employee emp = new Employee();
        emp.name = "John Doe"; // Inherited field
        emp.employeeId = 12345;
        System.out.println("Name: " + emp.name + ", Employee ID: " + emp.employeeId);
    }
}

In this example, the Employee class extends the Person class and inherits its name field. The Employee class also has its own field employeeId.

Tips and Best Practices

  • Use Inheritance Judiciously: Inheritance should be used when there is a clear "is-a" relationship between the subclass and the superclass.
  • Override Methods Properly: Use the @Override annotation when overriding methods to ensure that you are correctly overriding a method from the superclass.
  • Avoid Deep Inheritance Hierarchies: Deep inheritance hierarchies can make the code difficult to understand and maintain. Prefer composition over inheritance when appropriate.
  • Access Superclass Methods: Use super to call superclass methods from the subclass if needed.
    class Animal {
        void eat() {
            System.out.println("This animal eats food.");
        }
    }
    
    class Dog extends Animal {
        void eat() {
            super.eat(); // Call superclass method
            System.out.println("The dog eats dog food.");
        }
    }
  • Constructor Chaining: Use super() to call the constructor of the superclass from the subclass constructor.
    class Person {
        Person(String name) {
            System.out.println("Person's name is " + name);
        }
    }
    
    class Employee extends Person {
        Employee(String name, int employeeId) {
            super(name); // Call superclass constructor
            System.out.println("Employee ID is " + employeeId);
        }
    }

Understanding Inheritance

Inheritance is a key concept in object-oriented programming (OOP) that establishes a relationship between a more general superclass and a more specific subclass. This relationship allows the subclass to inherit attributes and methods from the superclass, promoting code reusability and logical structuring.

  • Is-A Relationship: The extends keyword signifies an "is-a" relationship. For example, a Dog is an Animal, and an Employee is a Person.
  • Single Inheritance: Java supports single inheritance, meaning a class can only extend one other class to avoid ambiguity and complexity.
  • Combining Extends and Implements: A class can extend another class and implement multiple interfaces simultaneously. This combination allows a class to inherit from a superclass and adhere to multiple contracts defined by interfaces.
class Animal {
    void move() {
        System.out.println("This animal moves.");
    }
}

interface Pet {
    void play();
}

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

    public void play() {
        System.out.println("The dog plays.");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.move();  // Inherited method from Animal
        dog.bark();  // Method of Dog class
        dog.play();  // Implemented method from Pet interface
    }
}

In this example, the Dog class extends the Animal class and implements the Pet interface, showcasing the combination of inheritance and interface implementation.