chevron_left
358 points
19 13 11

The implements and extends are two words that confuse many beginners when they start learning advanced programming in Java. Many beginners think these are used for the same purpose and can be used interchangeably, but this is not true. In this article, we are going to see the difference between these keywords and when to use what.

Implements:

The keyword implements is used when a class uses an interface. This keyword is used to implement an interface. If you do not know what an interface is, it is a collection of abstract methods that can be implemented by a class according to the requirement. 

Java does not support multiple inheritance i.e. deriving one class from two or more classes. So, to do multiple inheritance in Java, we use interfaces. The syntax for using this keyword is as follows:

class class_name implements interface_name
{
}

And if you want to implement multiple inheritance through interfaces, you can follow the following syntax:

class class_name implements interface1, interface2
{
}

Example:

interface Car
{
    public void Car_details();
}
class car1 implements Car
{
     public void Car_details()
    {
        System.out.println("Name: Audi \nColor: Black \nModel: 2020");
    }
}
public class Main //main class or driver class
{
   
    public static void main(String[] args)  //main() method
    {
        car1 c = new car1();
        c.Car_details();
    }
}

Output:

Name: Audi 
Color: Black 
Model: 2020

Extends:

The keyword extends is used to inherit or derive a class from another class. Through this keyword, you can easily reuse the methods and data of the parent class in a child class. Unlike interfaces, when you extend a class, you do not need to define the functions defined in the parent class. The syntax for using this keyword is as follows:

class child_class_name extends parent_class_name
{
}

You cannot achieve the functionality of multiple inheritance using the extends keyword. Consider the following example to understand how to inherit a class:

class Vehicle  //parent class
{
    public void Car_details(String car_name, String color, String model)
    {
        System.out.println("Name: " + car_name + "\nColor: " + color + "\nModel: " + model);
    }
}
class Car extends Vehicle  //child class
{
    
    public Car(String car, String clr, String mod)  //constructor
    {
        Car_details(car, clr, mod);   //calling parent's method inside constructor
    }
}

public class Main //main class or driver class
{
   
    public static void main(String[] args)  //main() method
    {
        Car car = new Car("Audi", "Black", "2020");  //creating an instance of child class
        System.out.println("\n");
        car.Car_details("Mercedes", "White", "2020"); //calling the parent's method with child's object
    }
}


Output:

Name: Audi
Color: Black
Model: 2020


Name: Mercedes
Color: White
Model: 2020

Difference Between extends and implements:

extendsimplements
used to inherit classesused to implement the interfaces
cannot be used for multiple inheritancemultiple inheritance can be achieved by implementing the interfaces
do not need to redefine any methodall the abstract methods must be defined

 

More Posts

Difference between implements and extends in java Mubashir Islam - Oct 26, 2020
What is difference between abstract class and interface in java kesav.eee - Jun 1
Difference between abstract and interface in java Sanjana Sagar - May 28
Difference between abstract and interface in java Sanjana Sagar - May 28
What is the difference between a class and an interface mhfaisal - Jun 12
What is difference between interface and abstract classes? Sumran Ali - May 9
Generalization and Specialization in Java with Example mhfaisal - Jul 4
for any kind of method in an interface, code implementation is absolutely not allowed - Java DivyaN - Sep 4
Inheritance in Java Yogesh Sharma - Aug 20
Order of Execution of Constructors in Java Inheritance Hafsa_Rao - Jun 19