This week's giveaway is in the Testing forum.
We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line!
See this thread for details.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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: 13826
312
  • 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: 75637
354
  • 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: 22644
123
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.
 
pie. tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic