| Author |
Interface vs abstract classes, when we can choose either of them?
|
Vinod Vinu
Ranch Hand
Joined: Aug 30, 2009
Posts: 217
|
|
Hi, i know what interfaces are and what abstract classes are. Like for e.g. suppose you have a common loggin for all the users then for that you can create a non-abstract method and put the login code in it and for non-common thing you can do through abstract method in Abstract Classes.
But for interfaces we just have abstract methods only and no non-abstract methods.
Someone asked me if you don't have common thing like i said using non-abstract methods then which will you choose an Interface or an Abstract ???
I told to go for interfaces but without any confidence.
Please tell me your views on it
I would really really appreciate your help
thanks
|
Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3065
|
|
Personally, I think you should almost always go with an interface. Abstract classes have their place, but when in doubt, use an interface.
When you have some standard implementation for a method, you can then make an abstract class implementing an interface. This leaves classes free to either implement the interfaces in a completely new way, or to extend the abstract class and inherit the standard implementation.
One of the greatest mistakes in the standard API in my opinion, is that InputStream and OutputStream were made abstract classes, instead of interfaces.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32827
|
|
|
Please look through this forum and our FAQ; then tell us what you don't understand.
|
 |
jishnu dasgupta
Ranch Hand
Joined: Mar 11, 2011
Posts: 103
|
|
I feel that an interface is lot more useful than an abstract class, if only abstract methods were taken into considerations as It gets my class the freedom to extend some other class or implement other interfaces.
So if i have a lot of user specific task to be perfomed i can break them into several interfaces instead of a single abstract class and then make the users implemet the interface that is most applicable to them
|
If debugging is the process of removing bugs, then programming must be the process of putting them in. -- Edsger Dijkstra
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19230
|
|
Stephan van Hulst wrote:Personally, I think you should almost always go with an interface. Abstract classes have their place, but when in doubt, use an interface.
When you have some standard implementation for a method, you can then make an abstract class implementing an interface. This leaves classes free to either implement the interfaces in a completely new way, or to extend the abstract class and inherit the standard implementation.
I agree. I usually create an interface, then if appropriate create one abstract class with basic implementations of the methods. Similar to List and AbstractList; Set and AbstractSet; Map and AbstractMap.
One of the greatest mistakes in the standard API in my opinion, is that InputStream and OutputStream were made abstract classes, instead of interfaces.
#1 on my rant list on the Java API; java.util.Observable is #2 (indexing based on when I came up with the items). InputStream and OutputStream should have been made interfaces, with the current implementations being renamed to AbstractInputStream / AbstractOutputStream. But of course that's not possible anymore, with too many classes (both in the API and created by others) already extending InputStream / OutputStream.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
 |
|
|
subject: Interface vs abstract classes, when we can choose either of them?
|
|
|