Monday Feb 11, 2008

Abstract class vs. Interface

At design-level, an interface describes the functionalites which could be implemented differently across various implementations. Therefore, it de-couples an architecture in terms of the functionalities. Whereas, an abstract class talks about the responsibilites of the classes under a hierarchy. It creates an inheritance hierarchy where subclasses provide
their own definitions for the inherited methods.


As an example, I might decide to write an interface called Accessor to access a data source.I would design GenericAccessor as an abstract class which implements Accessor. GenericAccessor could implement multiple interfaces to implement a range of functionalities which have been divided across multiple interfaces. So it means that GenericAccessor could be an Accessor and a Listener at the same time. If I were to design GenericAccessor as an abstract class, I would be putting all the functionalities inside the single class which
doesn't me incorporate re-usability into the design.

A few interesting facts:


Interface:
only final static variables are allowed to be declared.
all the declared methods are public
Abstract classes:
both instance and static variables are allowed
can have any kind of methods.
 

About

This is the blog of a software engineer, specialized in identity management. Kunal Sinha works in Directory Services Engineering (OpenDS) team from Austin,Texas.

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks