Abstract Class Vs Interface

Contract:

Often, an API is referred as a framework. We will consider a contract as an agreement that requires a developer to comply with the specification of an Application Programming Interface (API). As per English dictionary meaning of contract;


Contract

When using contracts, the developer is required to comply with the rules defined in the framework. This includes issues like method names, number of parameters, and so on. In short, standards are created to facilitate best practices for coding.

Enforcement is vital because it is always possible for a developer to breach the contract. But in the absence of a contract a rough developer could decide to reinvent the wheel and write her own code rather than use the specification provided by the framework. In Java and the .NET languages, there are two ways to implement contracts are to use abstract classes and interfaces.

Abstract Classes:

One way to implement contract is via abstract class. An abstract class is a class that contains one or more methods that do not have any implementation. Since every class is responsible for its own action but in this case there are some methods which have no implementation thus  they are abstract and that is why you cannot instantiate this class. Hence ‘abstract class cannot be instantiated’.

Let’s try to understand with an example. If I ask you to calculate an area, the first question you will be asking area of what? Circle or triangle or other. So the ‘CalculateArea’ is an abstract but area of circle or triangle not.  We can implement ‘calculateArea’ for circle and triangle separately.

We will try to understand using UML diagrams;

ContractRelationShip

This reflects the true polymorphic system. Any class using this Circle can use draw method and implement differently and get different result set.  Lets see in the form of code.

public abstract class Area {
public abstract double CaluclateArea();// no implementation for an abstract method
}

Note that this class does not provide any implementation for CaluclateArea method which makes this class abstract. There are two reasons it should be abstract, first Area does not know what kind of area I am going to calculate and;

Second, we want subclass of this base class to provide an implementation. i.e.;

public class Circle extends Area {
	double radius = 2.0;
	double PI=3.14;
@Override
public double CaluclateArea() {
	return PI*(radius * radius);
	}
}

To calculate Area of triangle you can use as follows.

public class Triangle extends Area{
	double base = 2;
	double height =4;
@Override
public double CaluclateArea() {
	return (base * height)/2;
	}
}

If you ‘extends’ Area in Circle and do not implement CalcualteArea method then code will not compile and ask you to override and implement this method.

Though the concept of an abstract class is all around abstract method, there is nothing stopping Area from actually providing some concrete methods with complete implementation. Which is common to all subclasses.  Just make sure if you have abstract methods in abstract class then you have to override and implement abstract method in a subclass. If you do not implement all abstract methods then base class should be abstract.

  • Remember

Abstract class is classified as strict inheritance, means is-a relationship works as opposed to interface  that is has-a relationship  which we will talk about  in the next topic.

Interface

I am sure you must want to ask a question that if we have abstract class that can provide the same functionality as an interface then why we need at the very first point.

Java and .NET does not provide multiple inheritance like C++. Java and .NET classes can inherit only one class so we can implement a number of interfaces.

Try to understand interface by UML diagram;

sigleRelationShip

Note that in UML diagram Nameable is identified as an interface which distinguishs it from class. It has two methods GetColor() and SetColor ().

public interface Colorable {
	String getColor();
	void(String colorName);
}

In the above code, notice that Colorable is not declared as  a class but as an interface. Thus, both methods, getColor() and setColor(), are considered abstract and there should be no implementation of any method in the interface.  You can point some difference between interface and abstract class.

  • An abstract class can have one or more abstract methods and methods with implementation while interface provides no implementation at all.
  • Subclass extends abstract class but implements the interface.
  • Subclasses have to implement all methods of interface else give compiler error while you may or may not implement all abstract methods from abstract classes.  In case if you are not implementing all method, you need to make a subclass as abstract class using abstract keyword.

Abstract class and Interface together

TBD:

Leave a Reply