Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between a Pure Abstract class and an Interface [closed]

Tags:

java

c#

oop

I was having a discussion with a coworker, who insisted that in Languages such as Java and C# there is never any reason to use a Pure Abstract base class, as it simply means you are unable to get around the lack of multiple inheritance.

I feel that he is wrong about this, as I've always thought that if a thing is a noun then it is an object, and if it is a verb then it is an interface.

For example, if I wanted to define type Bird, where I wanted to enforce the method fly without implementing it, then I would make that a pure abstract class.

If I wanted to define a type Flies, I would make it an interface with the method fly.

Bird might implement Flies.

Am I wrong?

EDIT:

The only solid argument I can give to support my point of view is that at some point in the future the design might need to change so that birds can eat. If all birds eat the same then this would need to be added to Bird, making it non pure abstract.

If Bird had been an interface this change would simply be a nightmare, since I cannot know whether things that inherit from some other base class also implement my Bird interface, so I can't just refactor my problems away.

like image 763
sji Avatar asked Sep 09 '25 21:09

sji


1 Answers

I can think of at least one good reason: You can extend abstract classes later on, without breaking backwards compatibility: Assume a class/interface

abstract class/interface Foo {
    void foo();
}

If we use an interface we now know for sure that there's no way to ever add additional functionality to Foo. This can lead to things like interface Foo2 implements Foo.

On the other hand if you have an abstract class you can easily add another method to it, as long as you provide a base implementation.

Note that Java8 will allow Interfaces to do basically the same thing - that'll be useful for library writers wanting to update their libraries to use lambdas without necessarily breaking compatibility to the millions of lines of code already written.

like image 109
Voo Avatar answered Sep 12 '25 11:09

Voo