Local database reading edition
Differences between interface & abstract class
Reconstructed from database records, not a captured original forum layout. Dates and display names are retained as recorded; timezone and real-world identity are not inferred. Historical advice, contact details and offers are not current. Original markup is shown as inert text.
Opening post
administrator · 2001-03-06 14:13:42
<h3>What are the differences between an interface and an abstract class? </h3>
Some use a semantic distinction: an abstract superclass models the "is" relationship, while an interface models the "has" relationship. The rule would be, if it's a subtype, inherit; otherwise, implement.
But, in the absence of real-world characteristics to distinguish the objects from their properties and parents, that becomes a circular argument. In this case, you have to look at the practical differences in Java (compared with C++).
Most differences between interfaces and abstract classes stem from three characteristics:
<ul>
<li> Both define method signatures that a derived class will have. </li>
<li> An abstract class can also define a partial implementation. </li>
<li> A class can implement many interfaces, but inherit from only one class. </li>
</ul>
In greater detail, these topics are:
<b>Method signatures</b> Both interfaces and abstract classes permit one to treat the derived-type class as the derived-from-type class. Both define a set of available methods in a way that can be enforced by the type-checking mechanism. This is typically used to permit different (derived) types to behave in the same way (as what they are derived from - i.e., they all support particular methods). For example, all java.* types can be printed as Strings because Object, the superclass of all java.* types, has a toString() method. Similarly, all types that implement the Observer interface can be passed an Observable to signal when an event has occurred. This permits an algorithm or service to operate on different (derived) types as if they were the same (derived-from) type.
This mechanism supports not only polymorphism (one object treated as another), but differentiation. In either case, the (derived) types can implement the method in the way appropriate to that type. However, you're not likely to override inherited functionality, but you must implement interface methods, so if you expect significant differentiation, then an interface might be warranted.
Finally, this mechanism supports a weak variant of access control. Only inherited methods are available to callers who only have a reference to the superclass or interface type. It's weak because they can attempt a narrowing cast if they know their target type. Nonetheless, it reduces some complexity.
<b>Inheriting</b> implementation Inheriting an implementation is useful where the code should be shared. This happens where derived types vary the functionality only a little bit, or where a complex set of method interfaces can through mutual reference be implemented with relatively few methods that can be implemented by derived types. You can also reuse code by having your class use or keep an object of another type that implements that code, but that doesn't permit your callers to treat you in a particular way. To both "get" functionality and to be treated "as" the superclass are the essentials of the type/subtype relationship.
<b>Java's rule of single inheritance</b> Java differs from C++ in permitting only single inheritance. This makes for some difficult choices, if you would like to share combinations of inherited functionality and polymorphism from more than one source. However, it does reinforce the notion of inheritance as a subtyping (is) relationship, and implicitly that type relationships form a tree rather than a network.
Other differences to consider:
<ul>
<li> Abstract class implementations may include fields </li>
<li> Interfaces may include final data members </li>
<li> It is slightly slower to call an implemented method via an interface reference. There is an even smaller penalty for calling a superclass method via a subclass reference (i.e., where the subclass does not override the method). There is almost no penalty for calling a subclass method via a superclass reference. (All are compared to a direct method call, i.e., calling the derived class method via a derived class reference).</li>
</ul>
Ahimanikya Satapathy
http://www.ammu.org/kuku
Source notes
MyCommunity/nandighosha.org.zip!/forum/tools/snitz_forums_2000.mdb
Table: FORUM_TOPICS + FORUM_REPLY · record IDs: 54