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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "when to use abstract class  and interface" Watch "when to use abstract class  and interface" New topic
Author

when to use abstract class and interface

vidhyasagar reddy
Ranch Hand

Joined: Jul 05, 2005
Posts: 40
can any body explain me when to use abstract class and when to use interface . i know the basic differences b/w them
Casper Maxwell
Ranch Hand

Joined: Aug 04, 2005
Posts: 88
As far as i remember, we can write method bodies into abstract classes:

...
public void printHello() {
System.out.println("Hello");
}
...

But we can not do this in interfaces. So when i have some common code to use between all specialized versions of my classes but also have interface requirements (inherited classes are required to have their own implementation of some methods), i will prefer to use abstract classes.

If there is no common functionality, i will just use an interface.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"Casper Casper",
Please see the warning about your display name here.

If you do not update your display name soon your account will be deleted

Dave
[Edit - ooops, wrong link]
[ August 10, 2005: Message edited by: David O'Meara ]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
http://faq.javaranch.com/view?InterfaceVsAbstractClass


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

Abstract classes vs. interfaces: When does it make sense to choose an abstract class over an interface?
Abstract classes and interfaces practicum: Move from theory to practice on when to employ abstract classes vs. interfaces
Tech Tips: ABSTRACT CLASSES VS. INTERFACES
Mahesh x Bogadi
Ranch Hand

Joined: Jul 06, 2004
Posts: 51
Simple rule.

* Use interface if you don't want to implement any of the member functions.
This implies.
- The class implementing the interface must implement all of its functions.

* In abstract class you need not define/implement all the functions like interface. This can be used where you can to define/implement some method and allow user to implement the rest(if there any unimplemented funtions..since there an abstract class can define/implement all the functions).

Interface and abstract class can be used for multiple inhertence. (OOP concept)
 
wood burning stoves
 
subject: when to use abstract class and interface