aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes choice between absract class and interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "choice between absract class and interface?" Watch "choice between absract class and interface?" New topic
Author

choice between absract class and interface?

sunil kumre
Ranch Hand

Joined: Dec 23, 2000
Posts: 64
What factors would decide as to whether an abstract class should be used instaed of an interface or vice-a-versa?
thanks in advance
sunil.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
It is better to use an interface as the root of your tree of classes. This provides you for multiple inheritance. If you need to provide some common implementation to them, extends your classes from an abstract one that implements the interface.


SCJP2. Please Indent your code using UBB Code
md ali
Greenhorn

Joined: Sep 06, 2001
Posts: 25
I think you should use interfaces when you want to define some genral behaviour of your programm. And your are not sure what will be the final way of implementing that general behavoiur in your application. Or if you want to have possibilty on implementing the same thing in more than one way then it is better to use interfaces. While abstarct classes can be used at places where you are sure about some part of program that how it is going to be implemented finally. And that way of implementing that part is the only way you need to implement it. Then you can use abstract classes.
For example, In one of my application I was to implement a logging mechanism. The logging mechanism was to be designed in a way that it should be able to log both in an oracle database and in file system depending upon the mode selected by user. So I implemented a top level interface which defined of opening, logging the information and closing of the underlying loggin system. And then I used two classes one for database loggin and other for file logging.
The above thing can also be extended to writing logging information to some specific port using Sockets.
While in the same project I implemented logging for another part of my programm there I used abstract class as my base class because I was knowing there were some operations common which I would have to perform in any case. So if I would not have taken an abstract class then I would have to code the same thing at two places. Once for database logging class and one for file logging class.
So in short where you do not know anything about the way you will implement a problem finally use interfaces. And where you know that something is common that is going to be implemented in any case in a specific way for your problem use abstract class.
Hope this helps.
Muhammad Ali
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: choice between absract class and interface?