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
Author

abstract and interfaces

hemapriya rao
Greenhorn

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

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
hemapriya rao
Greenhorn

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
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.

Example:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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
Greenhorn

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: http://aspose.com/file-tools
 
subject: abstract and interfaces
 
Similar Threads
Tech Word Game
ABSTRACT
WA #1.....word association
difference between BufferedInputStream(or output) and other i/ostreams,regarding buffers.
Reg. native methods