aspose file tools*
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
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: 19760
    
  20

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: 19760
    
  20

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: 14342
    
  22

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 8 API documentation
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: abstract and interfaces