This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why to use interfaces insted of Abstract Classes in some places?

 
Pras Tiwari
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,
As i know Interfaces are same as abstract classes (if we include only abstract methods in abstract class). Then why there are two different things in java(interfeces n classes), though Abstract classes can do functions of interfeces?
I mean to say why interface came in java, though there is abstract class for same purpose?
Is it due to "MULTIPLE INHERITANCE" limitation of Classes?
PLease reply...
pras
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Prashant,

Please refer to following article for a detail understanding on Interface Vs Abstract Class uses.

Design Principles from Design Patterns

The GOF authers Erich Gamma himself explains there thinking and concept behind "Program to Interface not to Implementation".

I found this so far a best explaination for Interface and Abstract Class.

Regards
Naveen
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prashant Tiwari:

Is it due to "MULTIPLE INHERITANCE" limitation of Classes?


Yes, that is exactly the only reason for the existance of interfaces in Java.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Naveen K Garg:

The GOF authers Erich Gamma himself explains there thinking and concept behind "Program to Interface not to Implementation".

I found this so far a best explaination for Interface and Abstract Class.


Read it again - Erich explicitely says that "programming to the interface" can be done using abstract classes.

In languages that support multiple class inheritance, purely abstract classes are indistinguishable from Java interfaces.
 
Naveen K Garg
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ilja,

I 100% agree with you.

But based my experience with various people, I found that they offten compare the general Interface term with Java Interface and hence the comfusion arise.

The link that I have give here does explain this difference.

Regards
Naveen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic