• 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

Abstract class / Interface

 
Ranch Hand
Posts: 597
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to choose between Abstract class or interface ? (Interview question)

I answered if you have few methods which have concrete implementation and few methods without implementation then go for abstract class, if you all the methods without implementation, go for interface.

But interviewer was not satisfied with this answer. Is there any other factor to choose between abstract class and interface.
 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and one more point you need to tell

if we want to achieve multiple inheritence then go for interface
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In general, prefer interfaces to represent type hierarchies. Use abstract classes mainly as convenient, default implementations of those interfaces.
 
Sandeep Awasthi
Ranch Hand
Posts: 597
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your replies. I dont think we can add anything more to this. I dont know what exactly the interviewer was expecting. Once again thanks for replies.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey !! read this, might interviewer expect this from you...

if you want to have your class methods to must have implementation by all subclasses then go for abstract classes...but wait...it seems same for interface...no ..
actually we use interface to filter out methods that should be implemented by subclasses on the discretion of the subclass ,confused?
consider example...

you have a class shape that have public method animate(). OK . now consider subclasses rectangle , square , circle subclases of class shape and all subclases have method animate(). hold on now you want to have only specific subclasses to have method ROTATE(), now what, if you put that rotate method() in superclass then all subclasses have that method but you want to filter out....so you declare an interface and declare a method in that and let subclasses implement that interface along with extending shape class... so square and rectangle implements the interface but circle may not.... may be it is cleared to you....
come up with queries if you not understand what i mean to tell you.....

Regards
Atul
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Summarized:

Abstract elements describes required elements of a type hierachy. When subclassed, you must implement them all. You can extend from only one superclass.

Interface elements describes optional elements of a type hierachy. It's your choice whether to implement them. You can implement more than one interface.
 
Sandeep Awasthi
Ranch Hand
Posts: 597
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much Atual and Bauke. I got it now.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic