doubt
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   guru_prasanth
Posted On:   Tuesday, April 15, 2003 05:44 AM

what are all the difference between using abstract class and interface?

Re: doubt

Posted By:   David_Bates  
Posted On:   Tuesday, April 15, 2003 06:14 AM

Abstract Classes:

  • A class may extend only one class.

  • An abstract class may have instance variables.

  • An abstract class may contain non-abstract methods i.e. methods with implementation defined

  • The visibility of methods can be private, protected, package or public. Other modifiers including final and static are allowed.



Iterfaces:

  • A class may implement many interfaces. Interfaces may only extend interfaces.

  • All variables are public static final. There are no instance variables

  • No definitions (i.e. implementation) of methods may be present in the iterface

  • All methods are public and abstract. No methods may be static. The final modifier is not allowed.





This is a bit of an aside, but it might give some context. It is quite common to define an interface and then have an abstract class which simply implements the interface, but contains empty method definitions. This is used a lot in Swing where you have the MouseListener interface which defines methods:

public void mouseDown()
... // and lots of other methods.

You then have the MouseAdapter:

public abstract class MouseAdapter {
public void mouseDown() {
}
... // and lots of other methods.
}

This allows the programmer to simply extend MouseAdapter and simply override the methods that are needed.
About | Sitemap | Contact