Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do we use interfaces just for multiple inheritence?

 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only reason i could find, why to use interfaces over abstract class is that

it support multiple inheritance,

is this conclusion correct?

> In Interfaces, final fields and abstract methods are there by default and need to implement them.
> In Abstract Class too, we can declare both things(can give some behavior too) that is final fields and abstract method and can extend them for implementation.

What other difference to use interface over abstract class?

On Internet mostly says that i use interfaces when there is behavior which is common all over to particular type, but abstract classes can also be used there.
could anyone explain me or provide such link?

 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd look at it the other way around. Why would you use an abstract class when you could use an interface? I'd use an abstract class only if I needed it to contain some implementation.
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically Java does not support multiple inheritance. We can only extend one class. This is not a bad thing imo.

Interfaces allow for cleaner code reduced coupling and a lot of design patterns depend heavily on them.

Per Joshua Boch, Effective Java , the idea is use an interface that way nobody is forced to extend a class (which you can extend only one) and developers can provide their own implementation if they want. Abstract classes are typically partial implementations to complex interfaces.
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:I'd look at it the other way around. Why would you use an abstract class when you could use an interface? I'd use an abstract class only if I needed it to contain some implementation.


actually some of my mate stick to this point that if we have abstract classes why interfaces then? he was giving the feel that abstract classes concept come before interfaces? still my reply to him was jst multiple inheritence to make one and implement many times

but i get confuse when he stick on same part why not abstract class
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Gorder wrote:Technically Java does not support multiple inheritance. We can only extend one class. This is not a bad thing imo.

Interfaces allow for cleaner code reduced coupling and a lot of design patterns depend heavily on them.

Per Joshua Boch, Effective Java , the idea is use an interface that way nobody is forced to extend a class (which you can extend only one) and developers can provide their own implementation if they want. Abstract classes are typically partial implementations to complex interfaces.


right tbank ufor valuable comments
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would he insist on abstract classes when you can only extend one class, but implement multiple interfaces? That doesn't make sense. Plus, you can simply turn the question around - since we have interfaces, why use abstract classes? If you look at Java code out there, you'll find that it's much more common to implement an interface than to extend some class, abstract or not.
 
Azrael Noor
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Why would he insist on abstract classes when you can only extend one class, but implement multiple interfaces? That doesn't make sense. Plus, you can simply turn the question around - since we have interfaces, why use abstract classes? If you look at Java code out there, you'll find that it's much more common to implement an interface than to extend some class, abstract or not.


Yupzz ULF you are correct

he insist on abstract class because he say abstract funda comes before interfaces, which i need to study. i haven't studied History
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of argument is that? Is Windows 3.1 better than Windows 7 just because it came earlier? (Not a perfect analogy, because in this case both have their place; but you get my drift.)
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> actually some of my mate stick to this point that if we have abstract classes why interfaces then? he was giving the feel that abstract classes concept come before interfaces?

I you want, you can think of the name "interface" as of a syntactic sugar for purely abstract classes - that is, ones essentially with constants and abstract methods only.
Given its role in Java, it was a good idea to give it a separate name.

 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:I you want, you can think of the name "interface" as of a syntactic sugar for purely abstract classes - that is, ones essentially with constants and abstract methods only.
Given its role in Java, it was a good idea to give it a separate name.


It's not just syntactic sugar, though, because of the multiple inheritance.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Azrael Noor wrote:actually some of my mate stick to this point that if we have abstract classes why interfaces then? he was giving the feel that abstract classes concept come before interfaces? still my reply to him was jst multiple inheritence to make one and implement many times


Well, the multiple inheritance is important. The difference between an interface and a pure (no implementation) abstract class is that interfaces are more flexible. So why wouldn't you use one in preference?

I would say that interfaces are more "fundamental" because they define a contract. That's all they do. Whereas an abstract class defines a contract and provides implementation. It's a combination of two concepts. So if you're thinking of "fundamental" in the way we speak of "fundamental particles" (i.e. things you can't break down any further), then interfaces are fundamental but abstract classes aren't. But this is all a bit of a sidetrack. Interfaces are more flexible - use them.

[Edited to fix my broken quote tags]
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
It's not just syntactic sugar, though, because of the multiple inheritance.


Well, the same stuff could be worded that multiple inheritance is supported provided all parent classes (with at most one exception) are pure abstract ones.
 
Winston Gutkowski
Bartender
Pie
Posts: 10254
59
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:Well, the same stuff could be worded that multiple inheritance is supported provided all parent classes (with at most one exception) are pure abstract ones.

Not quite. The major difference between an interface and an abstract class is that you cannot implement anything. An abstract class can.
The rules would be a lot more complex if we had to suffix everything that referred to abstract classes as being dependent on whether they were "purely abstract" or not.

I like Bloch's advice: Use interfaces to define types; use abstract classes to provide skeleton implementations. Never steered me wrong yet.

Winston
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in my experience i haven't seen abstract classes much. the only one i remember was an abstract Controller class that only provided "do nothing" methods for the interfaces it implemented.
 
Ivan Jozsef Balazs
Rancher
Posts: 972
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ivan Jozsef Balazs wrote:
Well, the same stuff could be worded that multiple inheritance is supported provided all parent classes (with at most one exception) are pure abstract ones.



I did not spell it out, but obviously I meant the "abstract class" expression not in the narrower Java but in a broader sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic