aspose file tools*
The moose likes Java in General and the fly likes Do we use interfaces just for multiple inheritence? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Do we use interfaces just for multiple inheritence?" Watch "Do we use interfaces just for multiple inheritence?" New topic
Author

Do we use interfaces just for multiple inheritence?

Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
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?


Regards
Azrael Noor
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Mar 07, 2010
Posts: 1680
    
    7

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.


[How To Ask Questions][Read before you PM me]
Azrael Noor
Ranch Hand

Joined: Jul 29, 2010
Posts: 382
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

Joined: Jul 29, 2010
Posts: 382
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
Marshal

Joined: Mar 22, 2005
Posts: 42934
    
  68
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

Joined: Jul 29, 2010
Posts: 382
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
Marshal

Joined: Mar 22, 2005
Posts: 42934
    
  68
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

Joined: May 22, 2012
Posts: 905
    
    5
> 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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: May 22, 2012
Posts: 905
    
    5
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
Ivan Jozsef Balazs
Rancher

Joined: May 22, 2012
Posts: 905
    
    5
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do we use interfaces just for multiple inheritence?