• 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

when to use abstract class and when to use an interface

 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q1)when to use abstract class and when to use an interface?

Q2) When i should use one over the other? with an example

Q3) Performance wise who will be faster?
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
use abstract class when you have some implementation that a group of subclass should use.
Also use abstract class to make sure that its instance cannot be created.

use an interface when you want all other classes to implement all methods in interface though those classes are not from same inheritance tree. interface generally defines a role that implementing classes should play.

and as for performance, i am not sure. may be both perform at same speed or may be its java virtual machine dependent. and why do you care about such a minute change in speed?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Generally it's consider better practice to use interfaces over abstract classes which follows the principle 'favor composition over inheritance'. But this is more like a rule of thumb and when there is a good reason for using an abstract class then go right ahead.
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when you want to force your subclass to implement all the methods of the superclass change it to interface.


Abstract classes are seldom in use
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this faq...
 
Ranch Hand
Posts: 71
Scala Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I find that abstract class is very useful to implement to Template Method pattern.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://radio.javaranch.com/ilja/2009/07/23/1248334442131.html.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

PrasannaKumar Sathiyanantham wrote:Abstract classes are seldom in use


Nothing could be further from the truth.
reply
    Bookmark Topic Watch Topic
  • New Topic