Java Extends class | Interface | Keyword | Multiple class Example

Java extends keyword is used in Java Inheritance. When one class wants to inherit the property of another class or one interface inherits the property of another interface, that time java code used a extends keyword. It’s a very important keyword in java programming.

Java Extends class Interface Keyword Multiple class Example

In this tutorial, we will learn about Java Extends class and Interface with many examples. But before the start, you must have basic knowledge about the below topics.

Short details about Inheritance: A Inheritance simply allows one class or interface to inherit the properties of another class or Inheritance.

Let’s Assume in your java program you have already one existing class as a Parent class. Now you want to create another class as a subclass. Where this subclass has the same methods and other property of Parent class (or other class). So you knew there is another class is also has the same class then why should write again and again the same code?

NO, you don’t need to write an again code in OOPs programming languages. You have to just use the extends keyword in the child class declaration which automatically makes every method available inside this class.

Note: there is one more Keyword “implements” used, which we will discuss in another tutorial.


How to use extend keyword in class syntax.

The syntax for the interface used extends keyword.

Java extends example

Here is an example of how to extends a class in java. Here Hello class extends Add class, so methods of Add class “addMethods” can use in Hello class with creating the object.

Output: Addition of number is 5

You can also Java Extends interface, here is an example of how to do Extends Interface in Java. But remember Interface can “extend” only interface not a class.

Extends multiple classes in Java

Some Time you need to inherit methods from 2 or more classes, at that time you needed multiple classes extends. Here is Java extends 2 (two) classes.

#First Way (Wrong)

If you are trying a Multiple Inheritance then it will not work, it’s not supported in java. See the below code with an error message if you extend 2 classes in java.

Java extends multiple classes error
#Second Way
Another way is to do it with a Multilevel inheritance. But still, this solution is not perfect because then the middle class has to do extra work and useless code. See below example.

Output: Addition of number is 5
Multiplication of number is 6


#Third  Way
A using an Interface can solve the problem but this time you have to use an implement keyword. Check the below program code of java extends implements.

This is the best solution we got for Java extends 2 classes, if you have any the do comment in below section.

Question. Can we use java extends and implements it at a time in class?

Answer: Yes we can. Here is an example of how to do it.

Complete example.

Do comment if you have any doubts and suggestions on this tutorial.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1

Java version 11

All Java extends class is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.