• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look through this forum and our FAQ; then tell us what you don't understand.
 
Ranch Hand
Posts: 103
Netbeans IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic