jump to navigation

Abstracts & Interfaces July 31, 2005

Posted by Coolguy in J2SE.

Abstract class

  • A class that is missing definitions for one or more methods.
  • You can’t thus create an object of that class.
  • You must first create a subclass and provide definitions for the abstract methods.
  • Abstract classes may implement some of the methods.
  • Though you can’t instantiate an abstract class, you can invoke its static methods.


  • Interface is a type that defines methods
  • All its methods must be abstract instance methods.
  • All its variables must be static final, i.e. constants
  • Classes that implement the interface implement the methods defined by the interface.
  • A class can implement multiple interfaces.
  • Interfaces can be used for
    • Capturing similarities among unrelated classes without artificially forcing a class relationship.
    • Declaring methods that one or more classes are expected to implement
    • Revealing an object’s programming interface without revealing its class
    • Modelling multiple inheritance

Interface Vs Abstract Class

feature interface abstract class
multiple inheritance A class may implement several interfaces. A class may extend only one abstract class.
default implementation An interface cannot provide any code at all, much less default code. An abstract class can provide complete code, default code, and/or just stubs that have to be overridden.
third party convenience An interface implementation may be added to any existing third party class. A third party class must be rewritten to extend only from the abstract class.
plug-in You can write a new replacement module for an interface that contains not one stick of code in common with the existing implementations. When you implement the inteface, you start from scratch without any default implementation. You have to obtain your tools from other classes; nothing comes with the interface other than a few constants. This gives you freedom to implement a radically different internal design. You must use the abstract class as-is for the code base, with all its attendant baggage, good or bad. The abstract class author has imposed structure on you. Depending on the cleverness of the author of the abstract class, this may be good or bad.
homogeneity If all the various implementaions share is the method signatures,then an interface works best. If the various implementations are all of a kind and share a common status and behaviour, usually an abstract class works best.If the various objects are all of-a-kind, and share a common state and behavior, then tend towards a common base class.
speed Slow, requires extra indirection to find the corresponding method in the actual class. Modern JVMs are discovering ways to reduce this speed penalty. Fast
adding functionality If you add a new method to an interface, you must track down all implementations of that interface in the universe and provide them with a concrete implementation of that method. If you add a new method to an abstract class, you have the option of providing a default implementation of it. Then all existing code will continue to work without change.



No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: