difference bet. abstract class and interface
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
hai friends,
this topic may look irrelevent to this session. hoping some experienced java programmers may visit this forum often, i am posting this question in this session.
i am not able to get a clear idea about the difference bet. interfaces and abstract classes.using both we can achieve virtual 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 class as well as interfaces while the intention can be achieved using oneterm itself.
please think in terms of oops rather than getting into the language.
what i have understood from the java books
{
abstract classes - implementations of some methods are allowed.
interfaces - method defn. not allowed.
using interfaces we can achieve multiple inheritance
}
in both the cases (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 it?
expecting your replies
with regards
balraj
this topic may look irrelevent to this session. hoping some experienced java programmers may visit this forum often, i am posting this question in this session.
i am not able to get a clear idea about the difference bet. interfaces and abstract classes.using both we can achieve virtual 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 class as well as interfaces while the intention can be achieved using oneterm itself.
please think in terms of oops rather than getting into the language.
what i have understood from the java books
{
abstract classes - implementations of some methods are allowed.
interfaces - method defn. not allowed.
using interfaces we can achieve multiple inheritance
}
in both the cases (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 it?
expecting your replies
with regards
balraj
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Multiple inheritance in C++ is poorly designed.
Interfaces are strictly for multiple inheritance. Whether you can accept that or not is up to you. If you think programmers are confused by the idea that an interface is a completely abstract class then I think you underestimate the intelligence of most oo developers.
Interfaces are strictly for multiple inheritance. Whether you can accept that or not is up to you. If you think programmers are confused by the idea that an interface is a completely abstract class then I think you underestimate the intelligence of most oo developers.
Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Interfaces are the "poor man's multiple inheritance" 
At least with inheritance you can inherit the implementation of 99 methods out of 100 (the 100th being the only abstract one which causes the whole Class to be abstract).
On the other hand, implementing Interfaces requires you to code the Interface method from scratch if your Class is not inheriting it from a superclass that implements it.

At least with inheritance you can inherit the implementation of 99 methods out of 100 (the 100th being the only abstract one which causes the whole Class to be abstract).
On the other hand, implementing Interfaces requires you to code the Interface method from scratch if your Class is not inheriting it from a superclass that implements it.
Tony Alicea
Senior Java Web Application Developer, SCPJ2, SCWCD
posted 25 years ago
Balraj,
An abstract class is a blueprint for a particular type of object , an interface is an abstract behaviour or attribute.
eg: animal is an abstract class. If you try to conceptualise an animal in your mind you would probably get an abstract image because its attributes and behaviour have not been
defined. It could be a lion , it could be a cockrach.
An action or behaviour like "move" is an interface. Move
could be swim , run , swing , crawl etc.
I hope this helps.
Rgds
Sahir
[This message has been edited by Sahir Shibley (edited March 27, 2001).]
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Balraj,
An abstract class is a blueprint for a particular type of object , an interface is an abstract behaviour or attribute.
eg: animal is an abstract class. If you try to conceptualise an animal in your mind you would probably get an abstract image because its attributes and behaviour have not been
defined. It could be a lion , it could be a cockrach.
An action or behaviour like "move" is an interface. Move
could be swim , run , swing , crawl etc.
I hope this helps.
Rgds
Sahir
[This message has been edited by Sahir Shibley (edited March 27, 2001).]
Sahir Shibley
Ranch Hand
Posts: 275
posted 25 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
?
[This message has been edited by Sahir Shibley (edited March 27, 2001).]
[This message has been edited by Sahir Shibley (edited March 27, 2001).]
| I think she's lovely. It's this tiny ad that called her crazy: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |










