HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Java Object Oriented Programming / Java extends vs implements keywords

Java extends vs implements keywords

In Java, extends is used for extending a class and implements is used for implementing the interfaces. It’s the main difference between extends vs implements.

1. extends keyword

In Java, we can inherit the fields and methods of a class by extending it using extends keyword. Please note that in Java, a class can extend maximum one class only.

Take the example of ArrayList class. It extends AbstractList class which in turn extends AbstractCollection class.

So essentially ArrayList class has methods and behaviors of both it’s parent classes AbstractList and AbstractCollection.

AbstractCollection provides methods like contains(Object o), toArray(), remove(Object o) etc. While AbstractList class provides add(), indexOf(), lastIndexOf(), clear() etc. Some of the methods are overridden by ArrayList again.

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
	//code
}

Java extends example

Let’s create something from scratch to better understand how Java inheritance using extends keyword works. I have created two classes – ParentClass and ChildClass, where ChildClass extends ParentClass.

public class ParentClass {
	
	public int dataVal = 100;
	
	public int getDataVal() {
		return this.dataVal;
	}
}
public class ChildClass extends ParentClass 
{
	
}

I have not added any field or method to ChildClass just to show that even if we don’t add anything to it, it still has behavior of it’s parent class.

public class Main 
{
	public static void main(String[] args) 
	{
		ChildClass child = new ChildClass();
		
		System.out.println( child.dataVal );
		System.out.println( child.getDataVal() );
	}
}

Program output.

100
100

2. implements keyword

Interfaces are way to enforce a contract in Java. They force the implementing class to provide a certain behavior. To implement an interface, class must use implements keyword.

In Java, we can implement more than one interfaces. In this case, class must implement all the methods from all the interfaces. (or declare itself abstract).

Look at the ArrayList class declaration one more time. It implements 4 interfaces i.e. List, RandomAccess, Cloneable and Serializable. It has implemented all the methods in given interfaces.

Java implements example

Similar to previous example, lets create something basic to understand how interface implementations look like. I have created two interfaces Movable and Swimmable. Both interfaces define one method.

A class Human implement both interfaces so it MUST implement the methods defined in both interfaces.

public interface Movable {
	
	public void move();
}
public interface Swimmable
{
	public void swim();
}

public class Human implements Movable, Swimmable 
{
	@Override
	public void swim() {
		System.out.println("I am swimming");
	}

	@Override
	public void move() {
		System.out.println("I am moving");
	}
}

Now we will test the human class and it’s enforced behavior.

public class Main 
{
	public static void main(String[] args) 
	{
		Human obj = new Human();
		
		obj.move();
		obj.swim();
	}
}

Program output.

I am moving
I am swimming

Clearly, Human class implemented both interfaces and defined their behavior. That’s whole purpose of interfaces in Java.

3. Differences between extends vs implements

Based on above examples, let’s list down the differences between extends and implements keywords in Java.

  1. extends keyword is used to inherit a class; while implements keyword is used to inherit the interfaces.
  2. A class can extend only one class; but can implement any number of interfaces.
  3. A subclass that extends a superclass may override some of the methods from superclass. A class must implement all the methods from interfaces.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Anand

    February 2, 2020

    “extends is used for extending a class and implements is used for implementing the interfaces”. But extends can also be used like – interface extends interface.

Comments are closed on this article!

Search Tutorials

Java OOP

  • OOP – Introduction
  • OOP – Access Modifiers
  • OOP – Constructors
  • OOP – Instance Initializers
  • OOP – Abstraction
  • OOP – Encapsulation
  • OOP – Inheritance
  • OOP – Polymorphism
  • OOP – Overloading vs Overriding
  • OOP – Interface vs Abstract Class
  • OOP – extends vs implements
  • OOP – instanceof operator
  • OOP – Multiple Inheritance
  • Association, Aggregation and Composition

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)