Site icon DataFlair

Java Extends vs Implements With Example Program

Java Extends vs Implements - Difference Between Java Implements and Extends

Java Extends vs Implements - Difference Between Java Implements and Extends

Free Java courses with 37 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.

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();
        }
  }

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.
Let’s Explore Java Generics – Class, Functions of Generics in Java

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

Exit mobile version