1
  1. An interface requires implementation of the specified functions in a class that implements the interface.

  2. An abstract function from an abstract class requires implementation in a class that extends the abstract class.

They are essentially the same things, and I don't really see the difference. I've had a look at similar questions, and the answers still don't make much sense.

Can someone explain all the differences in-depth, and practical examples of when you would use either?

Daniel
  • 121
  • 1
  • 6
  • 1
    Your question might be worded incorrectly. Did you mean "Difference between abstract method and interface method" or "Difference between abstract class and interface". For the former: Abstract methods can be made protected. Interface methods cant. – Jacques Koorts Mar 15 '19 at 12:44

2 Answers2

5

TL;DR: Interface is description, abstract class is (unfinished) implementation; with all consequences.

As already said, we should compare comparable things, so an abstract class (with all methods abstract) vs. interface.

An abstract class is a real class (albeit not instantiable, but usable as a superclass). So it is already materialized (abstract) skeleton of implementation. An implementation with holes to fill, so to speak.

Interface, on the other hand, is just a description of a contract. The difference is, interfaces are much more flexible to work with. A very typical example would immediately appear in any language with only single inheritance: your implementation class may conform to many interfaces at once, but you can only inherit from one class, not from many (no matter if they are abstract or not).

herby
  • 2,734
  • 1
  • 18
  • 25
2

An abstract method is one method. An abstract class can contain several methods, and so can an interface. The difference between those two is that an abstract class can have implementations for some of its methods, while an interface doesn't have any implementations.

(Usually. Java now allows default implementations even in interfaces, but try not to let that confuse you. Java is a bit of a special case, where the main difference between interfaces and abstract classes is that you can inherit only once, but can implement any number of times. Other languages with multiple inheritance don't have that limitation.)

Kilian Foth
  • 107,706
  • 45
  • 295
  • 310