Interfaces and Abstract Classes: Clearing the fog…..


There are two very important concepts or features in many Object Oriented Programming Languages that allow programmers to create a common interface to communicate with objects. They are Abstract Classes and Interfaces. They are fundamentally different but are used to provide the same functionality. Let us see about them separately and then we will see when to use an Interface and when to use an Abstract Class.

An Abstract Class is a class which does not provide concrete implementations (definitions) for its methods. Generally while creating a Class Hierarchy, we create a very general base class and derive these general members into a more specific Sub Class. I found the example in Paul and Harvey Deitel’s “C# for Programmers”  book very interesting and insightful.

A company wants to create an application to calculate salaries for the different types of employees it has, like Salaried Employees, Hourly Employee, Commissioned Employee. So basically you create a general Employee Class which will be abstract (to make a class Abstract just add the keyword ‘abstract’ before the keyword ‘class’) and put some general attributes and methods in it. But since the way the Salary is calculated differs from employee to employee, we can declare the Salary Calculation method as Abstract. When we declare a method as Abstract, we must not provide any concrete implementation for it (i.e) we must no provide definition for the method.

Java Syntax:

public abstract double salaryCalc();

C# Syntax:

public abstract double salaryCalc();

So now in the derived classes these methods can be “overridden” to provide employee-specific definitions/implementations.

Java Syntax:

public double salaryCalc() {

//definition

}

C# Syntax:

public override double salaryCalc() {

//definition

}

(Note: C# requires usage of  “override” keyword. In Java its optional to use “@Override” annotation above the method implementation)

Thus each derived class can provide its own implementation for the derived abstract method but all those methods will have the same signature/header/call syntax. So we provide uniform interface for accessing the methods of the derived class.

Speaking of providing common interface for accessing derived class, we can use a Class variable of the Abstract base class to polymorphically access the overridden methods of the derived classes. This is called “Subtype Polymorphism“. Abstract classes were created to make general base classes but they also serve as a basis for implementing Polymorphism.

But Polymorphism can also be implemented by another technique called Interfaces. Lets see about the Java and C# interface, a relatively new concept…

An Interface is similar to an Abstract Class, but differs in the fact that it provides absolutely no concrete implementation for any of its methods. Some may say an Interface is also a class. But its not. An Interface is not a class. Its like a contract that classes sign. When a class ‘implements’ an interface, it basically signs a contract saying that, “Ill provide definition for all the method declarations declared in the Interface”. Creating an interface is rather simple and similar to creating a class.

Java, C# Syntax:

public interface Employee {

double salaryCalc();

}

So now our employee specific classes can implement this interface and provide concrete definitions for the method salaryCalc(). In C#, syntax for implementing an interface is similar to that of inheriting a base class. But in Java instead of using the “extends” keyword which we use in order derive from a base class, we use the keyword “implements”.

And the classes that implement an interface can provide definitions for the interface methods just like creating a normal method. (i.e) There is no need to use the “override” key word (in case of C#). In Java however, its optional to include the ‘@Implements’ annotation above the method.

In the Abstract Class section, we implemented Subtype Polymorphism by creating  a Class Variable for base class and accessing the derived class instances/objects using that variable. Similar to that we can create an Interface Variable for the Interface and we can use that interface variable to refer the instances of the classes that implement that interface.

So now a question arises. Both abstract class and Interface provide same functionality and help implement polymorphism. So why do we need the new concept called Interfaces. There are two main reasons why we need both Interfaces and Abstract Classes.

The first reason is, In C# and Java, there is no such thing as ‘Multiple Inheritance’. This concept, which was included in languages like C++, caused lots of ambiguity and complexity and hence it was removed in the newer languages like C# and Java. And so a class can ‘extend’ or ‘inherit from’ only one base class. But there are some situations where you need something like multiple inheritance. So in those situations you may use an Interface. A class can implement ‘n number of Interfaces’

Id like to explain the second reason from the Polymorphism perspective. In polymorphism, you basically handle objects of multiple classes using a common Variable. But if you have only Abstract Class concept, only those classes that extend/inherit that abstract class can be handled using the common Variable (Class variable of the Abstract Base Class). But these classes that extend the Abstract Base class almost always come under the same category. (eg.) An employee Abstract Base class is extended by classes that represent some specific employees. An IO Device Base class is extended only by specific IO device classes. So if you have only Abstract Class concept, you can access multiple objects with a common variable only if these objects belong to the same class hierarchy of the Abstract Base class. In a sense you can deal only with related classes.

But if you want to handle instances of Classes which are unrelated with a common variable, then you need to use Interfaces. For eg: A company wants to develop an application that processes payments. But they want the same application to process payments to employees (Salaries) and payment to invoices. So we create two different class hierarchies, one for invoices and one for employees. And we will have to handle instances of these two hierarchies with a common variable. So to do this, we can create an interface that has the common method (say, getPaymentAmount() ) and make the two hierarchies implement this interface. Now we can create a variable for this interface and use it to access the instances of the two unrelated classes.

So use Abstract Class when you want to access instances of classes belonging to same Class hierarchy Polymorphically (using common variable) and use Interfaces when you want to access instances of different and unrelated class hierarchies Polymorphically (using common variable).

So these are the Abstract Class and Interface Concepts. So I hope this article gave you a clear idea about these two powerful concepts.

If you have any doubts, clarifications, suggestions please leave a comment below…..

Steve Robinson.

/* Class hierarchy example take from Paul and Harvey Deitel’s “C# for Programmers” 
UML Diagrams created using creately.com‘s online UML drawing application */

2 thoughts on “Interfaces and Abstract Classes: Clearing the fog…..

  1. Amazing article about Abstract class and Interface. Well framed article for beginners.

    Please post more articles related to JAVA.

    Thanks.

    • Sure bro. I’m kinda busy with certain stuff now. I actually planned on explaining those confusing topics in Java in a simplified manner. But time hasnt permitted me to start the series. Will do it ASAP. Thank you very much!

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