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.
In this tutorial, we will learn about the Java Extends class and Interface with many examples. But before the start, you must have basic knowledge about the below topics.
- What is a Java Class and Declaration?
- Java Interface | Definition, Use, Methods | What is?
- Inheritance | Types of Inheritance in Java
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 the 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.
Syntax:
How to use extend keyword in class syntax.
public classOne extends ClassBTwo { }
The syntax for the interface used extends keyword.
public interface OtherInterface extends MyInterface { }
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.
class Add { static int addMethod(int a, int b) { return a + b; } } class Hello extends Add { public static void main(String[] args) { //calling a methods without using a instance int total = addMethod(2, 3); System.out.println("Addition of number is " + total); } }
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.
interface LivingThing{ public void eat(); } interface Dog extends LivingThing{ public void Bark(); }
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.
#Second Way
class Add { static int addMethod(int a, int b) { return a + b; } } class Multiply extends Add{ static int multMethod(int a, int b) { return a * b; } } class Hello extends Multiply { public static void main(String[] args) { //calling a methods without using a instance int total = addMethod(2, 3); int mult = multMethod(2,3); System.out.println("Addition of number is " + total); System.out.println("Multiplication of number is " + mult); } }
Output: Addition of number is 5
Multiplication of number is 6
interface Add { default int addMethod(int a, int b) { return a + b; } } interface Multiply extends Add { default int multMethod(int a, int b) { return a * b; } } class MyMath implements Add, Multiply { } class Hello extends MyMath{ public static void main(String[] args) { MyMath myMath = new MyMath(); int total = myMath.addMethod(2, 5); int mult = myMath.multMethod(3, 4); System.out.println("Addition of number is " + total); System.out.println("Multiplication of number is " + mult); } }
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.
class Hello extends Multiply implements Message{ .... }
Complete example.
interface Message { default void msg() { System.out.println("interface "); } } class Multiply { static int multMethod(int a, int b) { return a * b; } } class Hello extends Multiply implements Message{ public static void main(String[] args) { int mult = multMethod(3, 4); System.out.println("Multiplication of number is " + mult); Hello hello = new Hello(); hello.msg(); } @Override public void msg() { System.out.println("Java extends and implements examples"); } }
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.1Java version 11
All Java extends class is in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.