Photo by Kari Shea on Unsplash

Guide to extends vs. implements Keywords in Java

Firas Ahmed
Javarevisited

--

In Java, extends and implements are two keywords that are closely associated with inheritance and interfaces. Let’s discuss them more in detail.

extends

The purpose of extends keyword is to extend the functionality of one class to another. In other words, it’s used to inherit methods and properties of a class. The class that uses this keyword is called a subclass, child or derived class and the class that it inherits is called superclass, parent or base class.

The derived class inherits all non-private members of the base class and can either override the existing functionality of the methods or define an implementation of abstract methods (an abstract method does not have a body).

The syntax is:

public class Subclass extends SuperClass { 
/* code */
}

Let’s create a class called Publication:

public class Publication {
protected String name;
protected String publisher;

// no-args constructor
public Publication() {}

// constructor to initialize the variables once the object is created
public Publication(String name, String publisher) {
this.name = name;
this.publisher = publisher;
}

public void displayInfo() {
System.out.println("name: "…

--

--

Firas Ahmed
Javarevisited

Hey, I'm a backend developer and I love to share what I learn with the community.