This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes abstract and interfaces 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 » Java » Java in General
Bookmark "abstract and interfaces" Watch "abstract and interfaces" New topic

abstract and interfaces

hemapriya rao

Joined: Jun 27, 2007
Posts: 15
could any one tell me the exact difference between abstract and interface class
Rob Spoor

Joined: Oct 27, 2005
Posts: 19654

1) A class can extend only one abstract class but implement multiple interfaces.

2) An interface can only declare constants (any field) and public abstract methods (any method).
An abstract class can have anything other classes can have: private fields, instance fields, protected members, constructors, you name it. Unlike other classes, it can also have abstract methods. Unlike interfaces, these abstract methods can also be protected or have no access modifier.
Please note that they cannot be private - private methods cannot be overridden, and therefore private abstract methods would never get an implementation.

How To Ask Questions How To Answer Questions
hemapriya rao

Joined: Jun 27, 2007
Posts: 15
1)so,should a immediate subclass of abstract class implement(define) all methods of abstract class?

2)when the class implements interface ,should that class override all methods of the interface?
Rob Spoor

Joined: Oct 27, 2005
Posts: 19654

For both situations, a class must either implement all abstract methods (so also methods defined inside an interface the class is implementing), or be abstract itself.

If a class is not abstract, it must make sure that all remaining abstract methods must be implemented.

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

This is a frequently asked question.
See the answer in the JavaRanch FAQ: InterfaceVsAbstractClass

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Saurabh Gupta

Joined: Oct 06, 2007
Posts: 18
Like to add one point in term of use of Abstract class and Interface:-

It's generally considered good OO practice to specify the "contract" which you want to adhere to via Java interfaces.

Use abstract Java classes when you want to provide some standard base code but want/need to force the user's of your class to complete the implementation.
I agree. Here's the link:
subject: abstract and interfaces
Similar Threads
Tech Word Game
WA #1.....word association
difference between BufferedInputStream(or output) and other i/ostreams,regarding buffers.
Reg. native methods