This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes difference bet. interfaces and abstrct classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "difference bet. interfaces and abstrct classes" Watch "difference bet. interfaces and abstrct classes" New topic
Author

difference bet. interfaces and abstrct classes

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
hai friends,
i am not able to get a clear idea about the difference bet. interfaces and abstract classes.using both we can achieve vitual polymorphism in java.see in c++,we are achieving it using abstract methods/classes. code reusablity can also be achieved using both interfaces as well as abstract classes. so my question is that why java uses two terms abstract as well as interfaces while the intention can be achieved using onething itself.
please speak in terms of oops rather than getting into the language.
thank you
with regards
balraj
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
This answer is maybe a bit too simple, however, abstract classes can contain some implementation, interfaces cannot.
Interfaces can be described as abstract classes where all methods are abstract and all variables are final.
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 575
    
    7

balraj,
in purely oop terms, (java) abstract is way to define a method interface to a class without defining the method (ie virtual
method in c++), interface (java) is a way to define: both
a set of methods that must be defined and to allow for multiple inheiritance. - difficult to discuss in pure oop terms, since
multiple inheiritance is not a pure oop term.
steve
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
dear Brown and steve,
thank you for your replies. you have come nearer to my question to some extent.
what i have understood from both of you is that
{
abstract classes - partial implementation
interfaces - No implementation at all
using interfaces we achieve multiple inheritance
}
in both the classes (interface or abstract class), we can not create instances at all. so we are not at all going to use those partial implementation at all.ie no method calls of those partial implementation.
coming to multiple inheritance, sometimes the final derived class may have two or more copies of the same signature from its parent classes. i have enclosed the example coding.
interface A
{
int add(int a,int b);
}
interface B extends A
{
int subtract(int a,int b);
}
interface C extends A
{
int multiply(int a,int b);
}
class D implements B,C
{
public int add(int a,int b) { return a+b;}
public int subtract(int a,int b) {return a-b;}
public int multiply(int a,int b) {return a*b;}
}
class Try1
{
public static void main(String args[])
{
D d=new D();
System.out.println(d.add(3,4));
System.out.println(d.subtract(4,5));
System.out.println(d.multiply(6,7));
}
}
so in class D, which copy of add() method we are implementing either from B or C, we don't know.
it is avoided in c++, defining the class as virtual at the time of deriving.
it is avoided in java using interfaces without the programmer's knowledge.
so my final word is that
{
1.parial implementation or no implementation is not at all a matter.
2.multiple inheritance can be achieved by both if sun java developers has already thoght of it. so we can not accept that interfaces in java are only for multiple inheritance.
}
so my question is that
1. has java unnecessarily used two terms which confuses programmers.
2.assuming sun java developers are much more intelligent people than us, what is the exact intension behind in it?
expecting your replies
with regards
balraj
Graeme Brown
Ranch Hand

Joined: Oct 13, 2000
Posts: 193
Hi Balraj
I can't give you a definitive answer to your two questions, but your code illustrates quite well how interfaces get round multiple inheritance.
Remember that Java doesn't actually allow multiple inheritance as such, since neither A,B, or C can be instantiated it does it matter from which definition add() is used. Class D is still going to have to provide the one and only implementation of add().
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference bet. interfaces and abstrct classes