Extends Keyword in Java

The extends keyword in Java is used to establish a class inheritance relationship. Extends Java allows a class (known as the subclass or derived class) to inherit the properties and behaviors (fields and methods) of another class (known as the superclass or base class).

The syntax for using the extends keyword is as follows:

class SubclassName extends SuperclassName {
    // Class members and methods of SubclassName
}

Following is a breakdown of what this means:

  1. SubclassName: This is the name of the subclass that you want to create.
  2. SuperclassName: This is the name of the superclass from which you want the subclass to inherit.

By using the extends keyword, the subclass SubclassName inherits all the non-private members (fields and methods) from the superclass SuperclassName. This means that the subclass can access and use those inherited members as if they were defined within the subclass itself.

Extends keyword example

Following is a simple example to illustrate the use of extends:

class Animal {
    String name;
    
    void makeSound() {
        System.out.println("Some generic animal sound");
    }
}

class Dog extends Animal {
    // Dog class inherits the 'name' and 'makeSound()' method from Animal class
    
    void makeSound() {
        System.out.println("Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog();
        dog.name = "Buddy";
        dog.makeSound(); // Output: Woof!
    }
}

In this example, the Dog class extends the Animal class. It inherits the name field and the makeSound() method from the Animal class. The makeSound() method is overridden in the Dog class, which means it provides its own implementation of the method.

FAQs

What is the purpose of the extends keyword in Java?

The extends keyword in Java is used to establish inheritance between classes. It allows one class (the subclass or derived class) to inherit the properties and behaviors (fields and methods) of another class (the superclass or base class).

Can a subclass extend multiple classes in Java?

No, Java supports single inheritance, which means a subclass can extend only one superclass. However, a class can implement multiple interfaces, allowing it to inherit method signatures from multiple sources.

What is the difference between inheritance and implementing an interface using extends and implements?

When we use extends for inheritance, a subclass inherits fields and methods from a superclass, while implements is used to declare that a class will provide implementations for the methods defined in one or more interfaces. Inheritance establishes an “is-a” relationship, while implementing an interface establishes a “can-do” relationship.

Can a subclass override methods inherited from its superclass?

Yes, a subclass can override (provide its own implementation for) methods that it inherits from its superclass. This is a fundamental concept in object-oriented programming and allows for customization of behavior in the subclass.

Are constructors inherited by the subclass when using extends?

Constructors are not inherited by the subclass, but the subclass can call the constructor of the superclass using the super() keyword to initialize the inherited fields and perform additional initialization specific to the subclass.

Sharing Is Caring: