File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When to use Interfcae and abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When to use Interfcae and abstract class" Watch "When to use Interfcae and abstract class" New topic

When to use Interfcae and abstract class

Akhilesh Yadav
Ranch Hand

Joined: Apr 04, 2006
Posts: 46
Please tell the use of interfce and abstract class

when to use interface and when to use abstract class
Quintin Stephenson
Ranch Hand

Joined: Nov 16, 2006
Posts: 40
for a better understanding of interfaces read:

for a better understanding of abstract classes read:

hope these pages help.

If at first you don't succeed, try, try again. If you don't try you have failed.
Dilraj Singh

Joined: Apr 22, 2007
Posts: 8
Akhlesh Hi,

Use absract class:

1. when you are sure classes that supposed to extend the abstract class will always need to follow behaviour that you are providing.

2. you want users not to change this functionality in future in their derived classes.

for example, for car engines, you might want to make "BasicEngineFuntion" as a abstract class that defines following function :

1.CarborrettorSpark() {// make spark in carborretor when some condition is met}

If it is not clear let me know.
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
You can always rewrite an abstract class plus subclasses, without using an abstract class. You cannot always rewrite interfaces plus implementations as abstract classes. Therefore abstract classes are more limited. Their advantage is that they're easy to teach.

Another construct that's easy to teach, but causes problems in larger programs, is 'goto'. (Java doesn't support goto anyway). So just because something is easy to teach, doesn't mean it's going to be that useful.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
FAQ: InterfaceVsAbstractClass
Andy Morris
Ranch Hand

Joined: May 30, 2004
Posts: 78
I generally define interfaces for everything, and then when appropriate provide abstract implementations of those interfaces. This gives the choice to concrete classes to extend from an abstract class or completely re-implement the interface/contract. Either way, calling code only needs to depend on the interface, which is something that can be defined very early on and is not tied to a specific implementation.
[ April 27, 2007: Message edited by: Andy Morris ]
I agree. Here's the link:
subject: When to use Interfcae and abstract class
It's not a secret anymore!