Java/J2EE and SOA (346 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (17 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (343 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

What is the Difference Between Extends and Implements in Java?

Last updated on Nov 28,2019 5.4K Views

14 / 22 Blog from Java Collections

The keywords extends and implements, both are used to execute the Inheritance concept of Object-Oriented programming, yet, there is a subtle difference between them. This article on extends vs implements in Java will help you understand the major differences between these keywords.

The topics discussed in this article are:

To better understand the difference between extends and implements, you also need to learn and understand the difference between abstract class and interface in Java.

extends Keyword

When a subclass extends another class, it allows the subclass to inherit (ie. reuse) and override code defined in the supertype. In simple terms, using extends keyword, a newly created class (subclass) can inherit the features of an existing class (superclass). Also, it can override the methods defined in a superclass. A class can never extend more than one superclass in Java.  Here’s an example program demonstrating abstract class:

package MyPackage;

class A {
	String s;
	A(String s1)
	{
		s = s1;
	}
	void display()
	{
		System.out.println(s);
	}

}

 class B extends A {
	String l;
	B(String s1, String s2)
	{
		super(s1);
		l = s2;
	}
	void display()
	{
		super.display();
		System.out.println(l);
	}

}

class ExtendsExample{
    public static void main(String args[]) {

    	A ob = new B("Welcome", "To Edureka");
    	ob.display();
    		
    	}
    }

Output: Welcome
To Edureka

Explanation: In the above code, you can observe that class B has extended class A, it has access to display() method and has overridden the method display( ) defined in class A. This immense power comes by using the extends keyword.

implements Keyword

When a class implements an interface, it has to provide an implementation of all methods declared inside an interface. If the class doesn’t wish to provide implementation, it can declare itself as an abstract class. Also, an interface can never implement another interface as implementing means defining the methods and interface always have abstract methods so an interface can never implement another interface. Here’s an example program demonstrating abstract class:

package MyPackage;

interface XYZ{
	
	void display(String s);
	void show(int i);
}

class Demo implements XYZ{
	
	public void show(int i){
		System.out.println("integer value:" +i);
		}
	public void display(String s){
		System.out.println("string value:" +s);
	}
}

class ImplementExample {
	public static void main(String args[]) {
		
		XYZ d = new Demo();
		d.display("TechDifferences");
		d.show(2);

	}

}

Output:

string value:TechDifferences
integer value:2

In the above code, you can observe that the Demo class implements two methods declared in the interface XYZ.

From the above content, you might have noticed the key difference between extends and implements in Java. Now let’s go ahead and list out other differences.

extends vs implements

The table below lists the key differences between the keywords extends and implements.

Comparison FeaturesExtendsImplements

Implementation

A class can inherit another class, or an interface can inherit other interfaces using a keyword extends

A class can implement an interface using keyword implements

Method

The subclass that extends a superclass may or may not override all the methods in a superclass

The class implementing an interface has to implement all the methods of that interface.

Class

A class can extend only one superclass.

A class can implement any number of an interface at the same time

Interface

An interface can extend any number of interfaces

An interface can never implement any other interface

Well, now you the key differences between extends and implements in Java

This brings us to the end of this extends vs implements in Java article. We went through the major differences between extends and implements keywords. To conclude, both are used to execute the Inheritance concept of Java but in different ways.

Make sure you practice as much as possible and revert your experience.  

Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer. 

Got a question for us? Please mention it in the comments section of this ‘extends vs implements in Java’ and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDate
Java Certification Training Course

Class Starts on 28th January,2023

28th January

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 25th February,2023

25th February

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

What is the Difference Between Extends and Implements in Java?

edureka.co