0

Assume an abstract class has only abstract methods declared in it and the interface has abstract methods.

How does this affect the abstract method m?

abstract class A {
    abstract void m();
}

interface A {
    abstract void m();
}
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
Ajay Takur
  • 6,079
  • 5
  • 39
  • 55
  • There's no difference here, but there will be when you want to add other things. – Jeroen Vannevel Dec 11 '14 at 19:38
  • 1
    In your example, the first notable difference is the visibility of the `m` method in `class A` -- which is package private, unlike the one in the interface -- which is `public`. – Costi Ciudatu Dec 11 '14 at 19:39
  • This question is *not* about the difference between an interface and an abstract class. – Jeroen Vannevel Dec 11 '14 at 19:41
  • @JeroenVannevel How do you figure that? The question says "what is the difference between abstract class and interface..." – Dawood ibn Kareem Dec 11 '14 at 19:43
  • 1
    @DavidWallace: "... for a scenario like this" which refers to an abstract class and an interface both defining an abstract method. The way I see it, the question is about how this abstract method differs between both situations. – Jeroen Vannevel Dec 11 '14 at 19:45
  • yes Jeroen Vannevel. – Ajay Takur Dec 11 '14 at 19:48
  • I think the only reason why you would choose the abstract class versus an interface for such an example is to later enforce stuff on your subclasses. One example would be to allow yourself to add a constructor to the `A` class (a no-arg constructor, if you don't want to break all the subclasses at once) and perform some initialization that will be triggered by the instantiation of any subclass. Or you may decide to implement `Comparable` and so you'll be able to provide a `default` implementation (even in Java pre-8) by implementing `compareTo` in the `A` superclass. – Costi Ciudatu Dec 11 '14 at 19:50
  • The above was meant to be an answer, but the question was closed before I finished it. :) – Costi Ciudatu Dec 11 '14 at 19:52
  • @CostiCiudatu you can always put your answer on the original question that this one duplicated. – Dawood ibn Kareem Dec 11 '14 at 19:55
  • Well, @JeroenVannevel I think you just changed this to a different question. – Dawood ibn Kareem Dec 11 '14 at 19:57
  • @JeroenVannevel I totally agree with David Wallace, especially since my answer/comment makes no sense now :) – Costi Ciudatu Dec 11 '14 at 19:58
  • @DavidWallace: I doubt it, consider the OP indicated in the comments my take on it was correct. If it turns out it wasn't, I'll close it again of course. – Jeroen Vannevel Dec 11 '14 at 20:00
  • @DavidWallace: my answer is not really suitable for a broad question like the difference between the two, but only to "why would you ever choose an abstract class in this case?" -- as I understood this question initially. – Costi Ciudatu Dec 11 '14 at 20:05

2 Answers2

3

Even if the abstract class is a pure abstract, i.e. contains only abstract methods and does not contain state, your subclass extends this class and therefore you have single inheritance only.

However you can implement several interfaces.

Therefore if you want to force your classes to be a subclass of your A and not to be subclass of something else, use abstract class. Otherwise define interface.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
AlexR
  • 114,158
  • 16
  • 130
  • 208
1

Interfaces are also more useful when you don't exactly know if all your entities will need to override a specific method. Consider for instance birds, some can fly and some can't. You don't want to force overriding a fly() method in all Bird subclasses.

public abstract class Bird(){
  ...
  public abstract void eat(); //all birds eat
  public abstract void sleep(); //all birds sleep
  public abstract void fly(); //is wrong here since not all birds can fly
}

A more correct way would be to define interfaces for specific cases, so in this case

public interface Flyable{
    abstract void fly();
}

Then you can define your subclasses as birds that can fly or not based on the fact they implement the Flyable interface or not.

public class Eagle extends Bird implements Flyable{
   @override
   public void fly(); //needed
   -----
}

public class Ostrich extends Bird{
   //does't fly
}
MihaiC
  • 1,618
  • 1
  • 10
  • 14