You are on page 1of 4

Abstract Class versus Interface

Most of the time, when we start to design the class, we right away start by having an interface. but
as popularly discussed the frequent confusion lies in what to choose. abstract class or an interface.
firstly lets define and observe few key points on abstract class and interface.

abstract class vs. interface

abstract class : cannot be instantiated; containing abstract methods as well as private/public


methods (containing implementations) and properties; When inherited, the abstract method
need to be implemented and the public methods can be overridden.

Interface: cannot be instantiated. contains ONLY public abstract methods and properties. It
cannot contain any method with implementation. [We need not explicitly declare the methods and
properties as public and abstract]

public abstract class Operation

private int Z;

public abstract int X

get;

set;

public abstract int Y

get;

set;

public abstract int Add();

public int Substract(int x, int y)

return (x-y+z);

}
//Interface

public interface Operation

string X

get;

set;

string Y

get;

set;

void Add();

having defined what is abstract class interface, lets address the key issue of what to choose now.

when doing so two key terms come into discussion that is related to an entity, one is intention and
the other one is implementation. Intention means you know about the entity and also may have
idea about its state as well as behavior but don’t know about how its looks or works or may know
partially. Implementation means actual state and behavior of entity.

Lets take a small example:

As i write through this article, let me see whether i could take a POST as the example:

I write post to convey my understanding. To let other know about it i need to publish the posts i
write. the posts that i write can be reviewed or go through discussion.

from the problem statement, the key terms are Post, Review and Discussion. And oh ya...what are
these....entire nothing but Content.

The question we need to ask yourself now is what are the objects we intend to create. The entity
for which no object need to be created is the candidate to become an abstract class or interface

Here i would want to create the object for review, discussion and post. probably at this point, i do
not want to create an object of content. I would publish the content in the Post/Review/Discussion
that I create
normal class : Post, Review and Discussion.

abstract class/interface: Content

public abstract class Content

public void SaveDraft();

public void Publish()

//default save at periodic intervals to preserve the content on sudden crash of


browser

public class Post : Content

public override void SaveDraft()

//save as draft

public void Publish()

//save the content

so I chose it to be an abstract class as i new what need to be done by default. If probably i dint
know what was default behavior and every class had to implement on its own, then my choice
would have been an interface.

The other key thing is only one abstract class can be inherited whereas one can inherit multiple
Interfaces.

Also another key things that would affect the decision is "how frequent there is a change" in the
class that we decide on to be an interface or abstract class. If it is implemented as an interface,
then tomorrow if we add any new method, then all the class inheriting this interface HAS to
implement the interface, else it will break. In abstract class one have added the new method as an
non abstract class. CAN-DO and IS-A relationship is also define the deference between Interface
and abstract class.

Also abstract is fast then interface because interface requires extra in-direction.

Looking at the way we get the requirements, looks like Abstract class has an upper-hand in compare
to interface. Using interface having only advantage of multiple inheritance.

You might also like