wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes when to use abstract class and when to use an interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "when to use abstract class and when to use an interface" Watch "when to use abstract class and when to use an interface" New topic
Author

when to use abstract class and when to use an interface

Satyajeet Kadam
Ranch Hand

Joined: Oct 19, 2006
Posts: 215
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?
rushikesh sawant
Ranch Hand

Joined: Dec 22, 2009
Posts: 65
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?


SCJP 5.0 100%
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
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.


JDBCSupport - An easy to use, light-weight JDBC framework -
PrasannaKumar Sathiyanantham
Ranch Hand

Joined: Nov 12, 2009
Posts: 110
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


To err is human,
To forgive is not company policy
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Look at this faq...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Allan Cheong
Ranch Hand

Joined: Nov 12, 2009
Posts: 71

I find that abstract class is very useful to implement to Template Method pattern.


SCJP6, SCJD, OCJWCD5
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

http://radio.javaranch.com/ilja/2009/07/23/1248334442131.html.


http://muhammadkhojaye.blogspot.com/
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60800
    
  65

PrasannaKumar Sathiyanantham wrote:Abstract classes are seldom in use

Nothing could be further from the truth.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: when to use abstract class and when to use an interface
 
Similar Threads
use of abstact class and interface
When to use Interfcae and abstract class
Abstract Class & Interface
abstract class vs interface ?
forcing implementing methods