Extends vs Implements in Java

1. Introduction

In Java, extends and implements are keywords used for inheritance and implementation. While extends is used by a class to inherit properties and methods from another class (superclass), implements is used by a class to adhere to an interface or multiple interfaces, meaning it must implement all abstract methods defined in the interface. This fundamental concept of object-oriented programming (OOP) helps in code reusability and polymorphism. This blog post will discuss the difference between extends and implements through a simple example.

2. Program Steps

1. Define a superclass and an interface with some methods.

2. Create a subclass that extends the superclass and implements the interface.

3. Demonstrate method overriding and implementation in the subclass.

4. Create an instance of the subclass and call its methods to demonstrate inheritance and implementation.

3. Code Program

// Step 1: Defining a superclass
class Animal {
    void eat() {
        System.out.println("This animal eats food.");
    }
}

// Defining an interface
interface Pet {
    void play(); // Interface method (does not have a body)
}

// Step 2: Creating a subclass that extends a class and implements an interface
class Dog extends Animal implements Pet {
    // Step 3: Implementing interface method and overriding superclass method
    @Override
    void eat() {
        System.out.println("Dog eats meat.");
    }

    @Override
    public void play() {
        System.out.println("Dog plays with owner.");
    }
}

public class Main {
    public static void main(String[] args) {
        // Step 4: Creating an instance of the subclass and calling its methods
        Dog myDog = new Dog();
        myDog.eat(); // Demonstrates overriding
        myDog.play(); // Demonstrates interface method implementation
    }
}

Output:

Dog eats meat.
Dog plays with owner.

Explanation:

1. Superclass (Animal): Defines a general method eat() that describes a generic action applicable to all animals.

2. Interface (Pet): Declares an abstract method play() that any class implementing the Pet interface must define. Interfaces specify a contract that implementing classes must fulfill.

3. Subclass (Dog): Inherits from Animal using extends and commits to the Pet interface using implements. It provides specific implementations for the eat() and play() methods, demonstrating how a class can extend functionality from a superclass and adhere to an interface's contract.

4. The Dog class overrides the eat() method inherited from Animal to provide a specific implementation for dogs. It also implements the play() method defined by the Pet interface.

5. An instance of Dog is created, and calling its eat() and play() methods demonstrates both method overriding and interface method implementation in action.

6. This example highlights the use of extends for inheritance and implements for fulfilling interface contracts in Java, illustrating how they contribute to polymorphism and code reusability.

Comments