Prajwal Tuladhar’s Blog
 
programming, life and some random thoughts

Oct 10 2008

Abstract Class versus Interface

Published by Prajwal Tuladhar at 11:40 pm under .NET, C#, Patterns, Programming

In software engineering, an abstract type is a type in a nominative type system which is declared by the programmer, and which has the property that it contains no members which are also not members of some declared subtype.
Interface generally refers to an abstraction that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide multiple abstractions of itself. It may also provide a means of translation between entities which do not speak the same language, such as between a human and a computer. Because interfaces are a form of indirection, some additional overhead is incurred versus direct communication. -Wikipedia

In Java, .NET and PHP 5 there are three ways to create and object i.e. inheritance, composition and interface.

  • Inheritance defines “is-a” relationship. Example: Student and Programmer is a Person
  • Composition defines “has-a” relationship. Example: Class Student contains class Book
  • Interface only models the behavior of an object. Example: Student and Programmer are both Nameable and both may have actions.

In Java, .NET and PHP 5, multiple inheritance (child class with more than one parent class) is not allowed so, interface can be used as a powerful tool to separate implementation. Interface is not a strict “is a” relationship. Abstract class represents some sort of implementation and it is a strict “is a” relationship. For example: A dog is a mammal and a reptile is not a mammal showing strict relationship whereas both dog and reptile may be nameable and both may have some actions.

abstract interface example

As we know that interface and abstract class both provide abstract methods so, making the best use of them is crucial in the object oriented paradigm. Abstract class provides both concrete and abstract methods whereas interface provides only abstract methods. Lets go through an example:

Assume that we have an abstract class Person and its concrete implementation class Student and Programmer. We have an interface called IWork that may or may not be implemented by the concrete classes Student and Programmer. We also have a composite class Address, though it is not required while distinguishing between interface and abstract class, using composite class will help us to clarify the scenario.

Class Diagram

Abstract Interface Class Diagram

Abstract Interface Class Diagram

C# Code

Abstract Class - Person

Abstract Class - Person

Interface - IWork

Interface - IWork

Class Address

Class - Address

Class - Programmer

Class - Programmer

Class - Program

Class - Program

Conclusion

  • Classes in a strict inheritance relationship must be related.
  • Interfaces can be used for classes that are not related. In the above example, only Programmer class is implemented but it is not necessary that Student also implements the IWork interface.
  • An interface never provides any implementation, only behavior.

References

You can download the example from here.


 

Trackbacks

(Trackback URL)

close Reblog this comment
blog comments powered by Disqus

RSS Feed
Subscribe by email
Follow me @ Twitter