This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Abstract class and interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Abstract class and interface" Watch "Abstract class and interface" New topic

Abstract class and interface

Madhawi Maurya
Ranch Hand

Joined: Aug 22, 2007
Posts: 42
Please tell me which one is better either abstract class or interface? which one should use in any appliction program?

SCJP1.5 with 90%
Sujith Mahesh

Joined: Jul 11, 2007
Posts: 6
Abstract class can have both abstract methods and non abstract methods.
Interface methods are implicitly "public abstract".
Abstract class can have non static variables.
Interface variables are implicitly "public final static".

Depending on the above inforamtion you can choose which suits best for your application and implement it.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by sujith mg:
Depending on the above inforamtion you can choose which suits best for your application and implement it.

Well, sort-of, but it omits the most important difference, which is that a class can extend only one superclass (abstract or not), but can implement any number of interfaces.

Interfaces are preferred to abstract classes, in general. Use abstract classes only when an interface cannot give you what you need.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968

One of the SCJA exam objectives is "Programming to an Interface."

There is no exam objective called "Programming to an Abstract class."

It all depends upon the use and the scenario, but typically, we see that the greater the deal of 'abstraction' the better, and it doesn't get much more abstract and flexible than an Interface.

-Cameron McKenzie
I agree. Here's the link:
subject: Abstract class and interface
It's not a secret anymore!