1

I have read number of articles about differences between abstract classes and interface. can someone please specify the conceptual difference between the two, i am summarizing my understanding so far as:

When we talk about abstract classes we are defining characteristics of an object type; specifying what an object is.

When we talk about an interface and define capabilities that we promise to provide, we are talking about establishing a contract about what the object can do

Please refrain from using Car, Animal examples for differentiating between the two terms.

I am more interested to understand conceptually how abstract class and interface fits into the concept of abstraction. If someone could additionally talk about abstraction concept in relation to abstract class or interface

Community
  • 1
  • 1
greenHorn
  • 497
  • 1
  • 5
  • 16
  • 1
    There is a singular difference - an `abstract class` can hold state, an `interface` cannot. That is the single, and fundamental difference between the two. – Boris the Spider May 06 '18 at 17:44
  • P.S. Interface is not a class! – zlakad May 06 '18 at 17:47
  • Actually I've seen the exectly the same question today on SO – oreh May 06 '18 at 17:47
  • 1
    @ZubairNabi those old answers are no longer as relevant, interfaces can now hold implementation; just not state. – Boris the Spider May 06 '18 at 17:49
  • Short answer is that you cannot inherit from more than one abstract class but can implement multipy interfaces. For instance Linked list in java is a Queue and Deque but inherit only one abstract class AbstractList – oreh May 06 '18 at 17:50
  • @maxim: Thanks, I am aware about this concept. My question is more around abstraction – greenHorn May 06 '18 at 17:52
  • 1
    @maxim that's not really the short answer, it's an implementation decision related to my [earlier point](https://stackoverflow.com/questions/50202721/how-is-abstract-class-different-from-an-interface-in-terms-of-abstraction-concep#comment87422154_50202721). – Boris the Spider May 06 '18 at 17:52
  • One more core aspect of abstract classes is that you can implement a method but do not exploit it into API, hide it with protected or default modifiers. It's impossible with interfaces – oreh May 06 '18 at 18:39

1 Answers1

1

As far as "abstraction" is concerned, interfaces and abstract classes are very similar.

In Java, "abstraction" is made concrete (couldn't help using this oxymoron) by specified, but unimplemented behavior. In this regard, interfaces and abstract classes are the same. And the biggest sign is that no objects can be made directly from either.

What makes interfaces different from abstract classes has little to do with the concept of abstraction. For example, the fact that abstract classes can have state and constructors gives them some "concrete" nature, although this doesn't change much of their difference from interfaces, as far as "specified/unimplemented" behavior is concerned.

ernest_k
  • 44,416
  • 5
  • 53
  • 99