extends Java Keyword with Examples

The extends keyword is used in a class or interface declaration to indicate that the class or interface being declared is a subclass of the class or interface whose name follows the extends keyword.

extends Java Keyword Examples

In below example, we have created Animals superclass and Dog and Cat subclasses extends an Animals superclass.
abstract class Animals {
    /** All kind of animals eat food so make this common to all animals. */
    public void eat() {
        System.out.println(" Eating ..........");
    }

    /** The make different sounds. They will provide their own implementation */
    abstract void sound();
}

class Cat extends Animals {
    @Override
    void sound() {
        System.out.println("Meoww Meoww ........");
    }
}

class Dog extends Animals {
    @Override
    void sound() {
        System.out.println("Woof Woof ........");
    }
}
Note that interface can extends other interfaces like:
public interface A{
}

public interface B extends A {
}


Comments