Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

interface v/s abstract

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,
What criterias would govern the choice between an abstract class and an interface?
Thanks in advance.
Sunil
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a very frequently asked question. Please use the search feature to find some previous threads on the topic.
 
Sheriff
Posts: 6450
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest Inheritance vs. Delegation as being worth a read.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Think of how stupid the average person is. And how half of them are stupider than that. But who reads this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!