1

In the Effective Java book it is written:

Existing classes can be easily retrofitted to implement a new interface. All you have to do is add the required methods if they don’t yet exist and add an implements clause to the class declaration. For example, many existing classes were retrofitted to implement the Comparable interface when it was introduced into the platform. Existing classes cannot, in general, be retrofitted to extend a new abstract class. If you want to have two classes extend the same abstract class, you have to place the abstract class high up in the type hierarchy where it subclasses an ancestor of both classes. Unfortunately, this causes great collateral damage to the type hierarchy, forcing all descendants of the common ancestor to extend the new abstract class whether or not it is appropriate for them to do so.

I don't see a difference between an abstract class and an interface here. Just, we can replace the highlighted word class with interface and everything further is still true.

+--------------+
|Abstract Class|
+--------------+
   ^        ^ 
   |        |
+------+ +------+
|Class1| |Class2|
+------+ +------+


+--------------+
|   Interface  |
+--------------+
   ^        ^ 
   |        |
+------+ +------+
|Class1| |Class2|
+------+ +------+

What is a difference?

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
J. Doe
  • 125
  • 1
  • 9
  • The Java docs explain as well, https://docs.oracle.com/javase/tutorial/java/IandI/abstract.html –  Oct 24 '17 at 20:53
  • 1
    "***Existing*** classes cannot, in general, be retrofitted to extend a new abstract class.". ***existing*** is a keyword here. – PM 77-1 Oct 24 '17 at 20:54
  • 1
    In general **there are no difference** except one - class can extend only **one** abstract class, but implement **multiple** interfaces. – Oleg Cherednik Oct 24 '17 at 20:56
  • Related: [Prefer composition over inheritance?](https://stackoverflow.com/q/49002/223424) – 9000 Oct 24 '17 at 21:00
  • There are 3 key differences between abstract classes and interfaces: 1. Abstract classes can have non-static fields whereas interfaces can only have static fields. 2. Abstracts classes can have a combination of concrete and abstract methods unlike interfaces, which can only have abstract methods. 3. A class can only extend or "subclass" one other class. On the other hand, a class can implement as many interfaces as the developer wants. – BNeumann Oct 24 '17 at 21:08
  • 1
    Please stop vandalizing your posts. – Petter Friberg Dec 13 '17 at 20:30

2 Answers2

0

Abstract classes can have non-static variables.

jpell
  • 163
  • 1
  • 10
-1

There are some important differences between abstract class and interface. Some of them are listed below.

  1. Abstract class can have implementation in it but interface can't.

  2. In interface you can have only static final variable ( all the values declared in intrface is automatically constant and static I.e immutable variables ) but in abstract class you can have mutable variables.

  3. Abstract class can be extended and only one abstract class can use for this but you can implement multiple interface in a class.