Java Extends vs Implements With Example Program
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
1. Java Extends vs Implements – Objective
In our previous tutorial, we talked about Java Autoboxing and Unboxing. Here, we are going to learn about the difference between Java Extends vs Implements. First of all, we will discuss what is Java implements and Java extends. Moreover, we will discuss the Java Implements example and Java Extends example to learn the concept better.
So, let’s start Java Extends vs Implements.
2. Difference Between Java Implements and Extends
Java Extends: When you want to extend a subclass to be extended in inheritance that we use Java extends.
Java Implements: When an implement an interface, we use the keyword implement.
Extends vs Implements: In short, extends is for extending a class and implements are for implementing an interface.
As Java doesn’t support multiple inheritances and this problem overcomes by multiple interfaces.
Do you know the Difference Between Abstract Class and Interface in Java
Java Implements Example –
public interface ExampleInterface { Â Â Â public void doAction(); Â Â Â public String doThis(int number); Â } Â public class sub implements ExampleInterface { Â Â Â Â public void doAction() { Â Â Â Â } Â Â Â Â public String doThis(int number) { Â Â Â Â } Â }
Java Extends Example–
public class SuperClass {    public int getNb() {        return 1;     }     public int getNb2() {        return 2;     }  }  public class SubClass extends SuperClass {      @Override      public int getNb2() {        return 3;     }  }
Read About Java Comparator Interface – Working of Collections.Sort()
In this case –
Subclass s = new SubClass(); Â s.getNb(); Â s.getNb2(); Â SuperClass sup = new SuperClass(); Â sup.getNb(); Â sup.getNb2();
3. Example to Understand Java Implements vs Extends
class Person { String name; Person(String n) { name = "Person:Â " + n; } } class Mother extends Person { Mother(String n) { super(n); name = "Mother:Â " + n; } void FeedChildren() { System.out.println(name + " is feeding the kids ..."); } } class Wife extends Person { Wife(String n) { super(n); name = "Wife:Â " + n; } void CallHusband() { System.out.println(name + " is calling the husband ..."); } } public class Test { public static void main(String args[]) { Person p = new Person("Prerna"); Mother m = new Mother("Mahima"); Wife w = new Wife("Raani"); System.out.println("p is a " + p.name); System.out.println("m is a " + m.name); System.out.println("w is a " + w.name); m.FeedChildren(); w.CallHusband(); } }
- Here the base class is Person, Mother, and Wife but when Wife and Mother are same the same person and that is the person we are talking about.
Let’s Know About Serialization and Deserialization in Java
class WifeAndMother extends Wife, Mother
This won’t work, for this, we need to use interfaces. Let us look into the code –
class Person { String name; Person(String n) { name = "Person:Â " + n; } } interface Mother { public void FeedChildren(); } interface Wife { public void CallHusband(); } class WifeAndMother extends Person implements Wife, Mother { WifeAndMother(String n) { super(n); name = "Wife and mother:Â " + n; } public void FeedChildren() { System.out.println(name + " is feeding the children."); } public void CallHusband() { System.out.println(name + " is calling her husband."); } } public class Test { public static void main(String args[]) { Person p = new Person("Prerna"); WifeAndMother w = new WifeAndMother("Raani"); System.out.println("p is a " + p.name); System.out.println("w is a " + w.name); w.FeedChildren(); w.CallHusband(); } }
4. Conclusion
Hence, in this Java Extends vs Implements tutorial, we learned how and where to use implements and extends in Java, how they are helpful in our programs using examples. In conclusion, discuss examples of Java Implements and Java extends with a difference between Java Extends vs Implements. Furthermore, if you have any query feel free to ask in a comment section.
Related Topic-Â Jar File in JavaÂ
Did we exceed your expectations?
If Yes, share your valuable feedback on Google
Wrong statement : As Java doesn’t support multiple interfaces and this problem overcomes by using multiple interfaces
Right statementstatement : As Java doesn’t support multiple inheritance and this problem overcomes by using multiple interfaces
Hi Bhavesh,
Thanks for showing interest in Java Extends vs Implements and for observation. It was our typo mistake and we have made the necessary changes. Hope you like our other java tutorials.
Keep learning… keep doing…
Regards,
DataFlair
I dont understand what super(n) is in example 3 and 4. Could you please explain?
Hello Hisham,
We are glad our readers are trying to interact with us through this Java extends Vs Implements Tutorial. Here, with super(n), you make a call to the constructor of the parent class for the class Mother and Wife; this is the constructor of class Person. To this call, we pass the argument ‘n’, which is a string.
Hope, you find it useful!
Regards,
DataFlair