Implements vs extends When to use What s the difference

0 votes
Please explain in an easy to understand language or a link to some article.
Dec 30, 2020 in Java by Rajiv
• 8,870 points
1,970 views

1 answer to this question.

0 votes

Extends is for extending a class.

implements are for implementing an interface

The difference between an interface and a regular class is that in an interface you can not implement any of the declared methods. Only the class that "implements" the interface can implement the methods. The C++ equivalent of an interface would be an abstract class (not EXACTLY the same but pretty much).

Also, Java doesn't support multiple inheritances for classes. This is solved by using multiple interfaces.

 public interface ExampleInterface {
    public void doAction();
    public String doThis(int number);
 }

 public class sub implements ExampleInterface {
     public void doAction() {
       //specify what must happen
     }

     public String doThis(int number) {
       //specfiy what must happen
     }
 }

now extending a class

 public class SuperClass {
    public int getNb() {
         //specify what must happen
        return 1;
     }

     public int getNb2() {
         //specify what must happen
        return 2;
     }
 }

 public class SubClass extends SuperClass {
      //you can override the implementation
      @Override
      public int getNb2() {
        return 3;
     }
 }

in this case

  Subclass s = new SubClass();
  s.getNb(); //returns 1
  s.getNb2(); //returns 3

  SuperClass sup = new SuperClass();
  sup.getNb(); //returns 1
  sup.getNb2(); //returns 2

Also, note that an @Override tag is not required for implementing an interface, as there is nothing in the original interface methods to be overridden

I suggest you do some more research on dynamic binding, polymorphism and in general inheritance in Object-oriented programming

answered Dec 30, 2020 by Gitika
• 65,770 points

Related Questions In Java

0 votes
2 answers

What's the difference between @JoinColumn and mappedBy when using a JPA @OneToMany association?

JPA mapping annotation can be classified as ...READ MORE

answered Oct 28, 2020 in Java by bjjj
• 140 points
21,331 views
0 votes
2 answers

What's the best way to check if a String represents an integer in Java?

You can also use regular expression. str.matches("-?\\d+"); It will ...READ MORE

answered Aug 9, 2018 in Java by Daisy
• 8,140 points
3,956 views
0 votes
2 answers

When to use Static Methods in Java?

A static method has two main purposes: For ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
16,067 views
0 votes
1 answer

What is the difference between Runnable and extends Thread

Runnable is the preferred way to create ...READ MORE

answered May 1, 2018 in Java by sharth
• 3,370 points
1,154 views
0 votes
2 answers

What is the difference between implements and extends?

Extends : This is used to get attributes ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,220 points
16,218 views
0 votes
1 answer

using comparator for sorting in java

You've got two problems: 1) You're using Collections.sort (which takes ...READ MORE

answered Jul 6, 2018 in Java by samarth295
• 2,220 points
795 views
+1 vote
2 answers

Retrieve all the implementations of an interface in Java

Take a look to this example: https://github.com/burningwave/co ...READ MORE

answered Dec 21, 2019 in Java by Roberto
• 460 points

edited Jul 7, 2020 by Roberto 38,398 views
0 votes
1 answer

Usage of abstract class and interface

Well let me draw a clear line ...READ MORE

answered Oct 23, 2018 in Java by geek.erkami
• 2,680 points
727 views
0 votes
1 answer

When to use LinkedList over ArrayList in Java?

LinkedList and ArrayList are two different implementations of the List ...READ MORE

answered Dec 21, 2020 in Java by Gitika
• 65,770 points
1,832 views
0 votes
2 answers

When to use static methods

Java main() method is always static, so that compiler ...READ MORE

answered Dec 29, 2020 in Java by Reshma
998 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP