Java Extends vs Implements With Example Program

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.

Java Extends vs Implements

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();
      }
  }
This was all about Java Extends vs Implements Tutorial. Hope you like our explanation.

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 

For reference

4 Responses

  1. bhavesh says:

    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

    • DataFlair Team says:

      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

  2. Hisham Mohamed says:

    I dont understand what super(n) is in example 3 and 4. Could you please explain?

    • DataFlair Team says:

      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

Leave a Reply

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