Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface vs abstract classes, when we can choose either of them?

 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stephan van Hulst
Bartender
Pie
Posts: 6083
71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please look through this forum and our FAQ; then tell us what you don't understand.
 
jishnu dasgupta
Ranch Hand
Posts: 103
Eclipse IDE Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Pie
Posts: 20606
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic