File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes interface v/s abstract Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface v/s abstract " Watch "interface v/s abstract " New topic
Author

interface v/s abstract

sunil kumre
Ranch Hand

Joined: Dec 23, 2000
Posts: 64
Hello everyone,
What criterias would govern the choice between an abstract class and an interface?
Thanks in advance.
Sunil
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
This is a very frequently asked question. Please use the search feature to find some previous threads on the topic.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Jason Menard
Sheriff

Joined: Nov 09, 2000
Posts: 6450
I would suggest Inheritance vs. Delegation as being worth a read.
Trent DiBacco
Greenhorn

Joined: May 24, 2002
Posts: 18
this is the way i would look at it
Use abstract if you plane on make a class more specialized
example you want to make a class GermanShepard,
since GermanShepard is a more specilaized verison of a dog, and there are no such things as just dogs, there are only diffrent type of dogs the dog would make an exelent canidate ore abstraction
class dog{
...
}
class GermanShepard expands dog{
...
}
and use interface when you think many unrelated obj may posses a similar method or characteristic.
like say eyes, ears, nose, walk() talk().
then you might make and Anaimal interface
I know what your thinking, why not make Anaimal a abstarct class as well. You could, but providing and interface allow for you to right other classes that call on the interface methods of classes that you have yet to even dream of making.
It also give you the ability to implement many characteristics with multible interfaces, there is no mulitple inhertance so this is a big advatage.
so this is my little example
interface Anaimal{
String talk();
}
abstract class Dog implement Anaimal{
abstract String wagTail();
}
class GermanShepard expands Dog{

String talk(){
return "Woof Woof";
}
String wagTail(){
return "Up Down, Up Down";
}
}
//Notice i am implementing to interfaces
class Human implements Anaimal, Primate
{
String talk(){
return "Hello";
}
}
Notice that both the Human and the GermanShepard use the talk() method, yet only the dog wags a tail.
This wount compile but i hope you get the point, if not e-mail me and i will make a working example for you.
example
[ June 07, 2002: Message edited by: Trent DiBacco ]
Roberto Ingunza
Greenhorn

Joined: Mar 12, 2002
Posts: 4
As pointed out before, there is no multiple inheritance in Java, so you should choose carefully the parent class when designing your components. Usually is a matter of roles: a class have a primary role and multiple secondary roles.
One interesting way of defining when a role can be principal for a class is when you consider the attributes: interfaces usually define a behavior (i.e. methods) only as a signature, which means you do´┐Żn't expect anything in common from your objects more than comply with that signature. You can use abstract classes when:
1) You want to inherit non-constant attributes.
2) The parent class defines non-abstract methods.
In other words, if your abstract class have no use but to define a behavior (only abstract methods and constant attributes) then it's best to use it as an interface because it can't be a principal role.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface v/s abstract