How do I override a method in Java?

Method overriding lets a subclass provide its own implementation of an inherited instance method.

In this example, you will override the printDetails() method so an ebook displays its file format along with its title and author.

Source code

Save the following code as MethodOverridingExample.java:

public class MethodOverridingExample {
    void main() {
        Book book = new Book("Learning Java", "Alice");
        Book ebook = new Ebook("Exploring Java", "Bob", "PDF");

        book.printDetails();
        IO.println();
        ebook.printDetails();
    }
}

class Book {
    private final String title;
    private final String author;

    Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    public void printDetails() {
        IO.println("Title: " + title);
        IO.println("Author: " + author);
    }
}

class Ebook extends Book {
    private final String fileFormat;

    Ebook(String title, String author, String fileFormat) {
        super(title, author);
        this.fileFormat = fileFormat;
    }

    @Override
    public void printDetails() {
        super.printDetails();
        IO.println("File format: " + fileFormat);
    }
}

Explanation

The Book class defines a method that prints its title and author:

public void printDetails() {
    IO.println("Title: " + title);
    IO.println("Author: " + author);
}

The Ebook class extends Book and overrides that method:

@Override
public void printDetails() {
    super.printDetails();
    IO.println("File format: " + fileFormat);
}

The overriding method has the same name and parameter list as the inherited method. Here, it also has the same return type, void.

Using the @Override annotation

The @Override annotation tells the compiler that the method is intended to override an inherited method.

If you misspell the method name or use the wrong parameters, the compiler reports an error. This helps catch mistakes before running the program.

Calling the superclass implementation

Inside the overriding method, this statement calls the implementation in Book:

super.printDetails();

It prints the title and author. The next statement adds the ebook’s file format:

IO.println("File format: " + fileFormat);

Calling super.printDetails() is optional. This example uses it to reuse the existing printing code.

Selecting the method at runtime

The following variable has the declared type Book, but refers to an Ebook object:

Book ebook = new Ebook("Exploring Java", "Bob", "PDF");

When you call:

ebook.printDetails();

Java runs the implementation in Ebook because that is the object’s actual class. This behavior is called runtime polymorphism.

Run the example

Compile and run the file:

javac MethodOverridingExample.java
java MethodOverridingExample

Output

Title: Learning Java
Author: Alice

Title: Exploring Java
Author: Bob
File format: PDF

Overriding changes an inherited instance method’s behavior in a subclass. Overloading, covered earlier, defines methods with the same name but different parameter lists.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.