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.
Take the example of
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.
I have not added any field or method to
Program output.
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.
A class
Now we will test the human class and it’s enforced behavior.
Program output.
Clearly, Human class implemented both interfaces and defined their behavior. That’s whole purpose of interfaces in Java.
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 usingextends
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 { } |
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() ); } } |
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 interfacesMovable
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" ); } } |
public class Main { public static void main(String[] args) { Human obj = new Human(); obj.move(); obj.swim(); } } |
I am moving I am swimming |
3. Differences between extends vs implements
Based on above examples, let’s list down the differences between extends and implements keywords in Java.- extends keyword is used to inherit a class; while implements keyword is used to inherit the interfaces.
- A class can extend only one class; but can implement any number of interfaces.
- A subclass that extends a superclass may override some of the methods from superclass. A class must implement all the methods from interfaces.