Java Extends vs Implements

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.

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.

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.

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. yash

    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 Reply

Your email address will not be published. Required fields are marked *