Java Extends vs Implements

Java uses two keywords to inherit – extends and implements. The difference of them and their usage is trivial to understand.

Introduction Java Extends vs Implements

Go through the following combinations and rules one-by-one. Write small programs and practice. It is required to master them for a better Developer.

1. Inheritance between concrete (non-abstract) and abstract classes use extends keyword.

Precautions:
a) You must extend only one class to another, any number of classes in a ladder; and this is known as multilevel inheritance which Java supports happily.
b) You cannot extend multiple classes to one class and this is known as multiple inheritance which Java does not support through classes.

class A { }		       // permitted, this is multilevel inheritance
class B extends A { }
class C extends B { }

The above code is permitted as the inheritance is done through one-to-one in a ladder. In the above hierarchy of classes A, B and C, the classes may be a) all abstract classes or b) any one of two can be.

2. Inheritance between classes (including abstract classes) and interfaces, use implements keyword. Java support multiple inheritance with interfaces and for this reason, you can have multiple interfaces after implements keyword.

Precaution:

a) After extends there should be only one class (either concrete or abstract) and after implements there can be any number of interfaces.

interface A { }		        // permitted.  this is multiple inheritance through interfaces
interface B { }
class C { }                     // here, class C can be abstract class also
class D extends C implements A, B { }

Observe, after implements we are writing two interfaces and after extends only one class.

The above code can be modified as follows and works successfully.

interface A { }		
interface B extends A { }       // observe, extends between two interfaces, permitted
class C { }                     // here, class C can be abstract class also
class D extends C implements B { }

Remember, between two interfaces, we must use extends keyword only.

Pass your comments to improve the quality of this tutorial "Java Extends vs Implements".

4 thoughts on “Java Extends vs Implements”

  1. I was confused a lot for the first time when I hear this topic but now it is clear for me & I was happy.Thank You sir.

  2. Sir ,

    Thanks for the post . Being a beginner i was confused with implement and extend and this post made everything clear to me . Thanks again.

Leave a Comment

Your email address will not be published.