You are on page 1of 5

Abstract Classes and Methods

This is a detailed analysis of Abstract classes and methods in C# with some concrete examples.
The keyword abstract can be used with both classes and methods in C# to declare them as
abstract.
The classes, which we can't initialize, are known as abstract classes. They provide only partial
implementations. But another class can inherit from an abstract class and can create their
instances.
For example, an abstract class with a non-abstract method.
using System;

abstract class MyAbs


{
public void NonAbMethod()
{
Console.WriteLine("Non-Abstract Method");
}
}

class MyClass : MyAbs {}

class MyClient
{
public static void Main()
{
//MyAbs mb = new MyAbs();//not possible to create an instance

MyClass mc = new MyClass();


mc.NonAbMethod(); // Displays 'Non-Abstract Method'
}
}

An abstract class can contain abstract and non-abstract methods. When a class inherits from an
abstract, the derived class must implement all the abstract methods declared in the base class.
An abstract method is a method without any method body. They are implicitly virtual in C#.
using System;

abstract class MyAbs


{
public void NonAbMethod()
{
Console.WriteLine("Non-Abstract Method");
}
public abstract void AbMethod(); // An abstract method
}

class MyClass : MyAbs //must implement base class abstract methods


{
public override void AbMethod()
{
Console.WriteLine("Abstract method");
}
}

class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.NonAbMethod();
mc.AbMethod();
}
}

But by declaring the derived class also abstract, we can avoid the implementation of all or certain
abstract methods. This is what is known as partial implementation of an abstract class.
using System;

abstract class MyAbs


{
public abstract void AbMethod1();
public abstract void AbMethod2();
}

//Not necessary to implement all abstract methods


//Partial implementation is possible
abstract class MyClass1 : MyAbs
{
public override void AbMethod1()
{
Console.WriteLine("Abstract method #1");
}
}

class MyClass : MyClass1


{
public override void AbMethod2()
{
Console.WriteLine("Abstract method #2");
}
}

class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();

mc.AbMethod1();
mc.AbMethod2();
}
}

In C#, an abstract class can inherit from another non-abstract class. In addition to the methods it
inherited from the base class, it is possible to add new abstract and non-abstract methods as
showing below.
using System;

class MyClass1 // Non-Abstract class


{
public void Method1()
{
Console.WriteLine("Method of a non-abstract class");
}
}

abstract class MyAbs : MyClass1 // Inherits from an non-abstract class


{
public abstract void AbMethod1();
}

class MyClass : MyAbs//must implement base class abstract methods


{
public override void AbMethod1()
{
Console.WriteLine("Abstract method #1 of MyClass");
}
}

class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method1();
mc.AbMethod1();
}
}

An abstract class can also implement from an interface. In this case we must provide method
body for all methods it implemented from the interface.
using System;

interface IInterface
{

void Method1();
}

abstract class MyAbs : IInterface


{
public void Method1()
{
Console.WriteLine("Method implemented from the IInterface");
}
}

class MyClass : MyAbs//must implement base class abstract method


{}

class MyClient
{
public static void Main()
{
MyClass mc = new MyClass();
mc.Method1();
}
}

We can't use the keyword abstract along with sealed in C#, since a sealed class can't be abstract.
The abstract methods are implicitly virtual and hence they can't mark explicitly virtual in C#.
For example
using System;

abstract class MyAbs


{
public abstract void AbMethod1();
public abstract void AbMethod2();
}

class MyClass1 : MyAbs


{
public override void AbMethod1()
{
Console.WriteLine("Abstract method #1 of MyClass1");
}
public override void AbMethod2()
{
Console.WriteLine("Abstract method #2 of MyClass1");
}
}

class MyClient
{
public static void Main()
{
MyAbs ma1 = new MyClass1();// Polymorphism
ma1.AbMethod1();
ma1.AbMethod2();
}
}

NOTE: Abstract Class and Interface define a set of behavior for a class

Interface
In C# an interface definition looks like this:

public interface IstringList


{
void Add(string s); // A method
int Count {get; } // A read-only property
event EventHandler Changed;// An event
string this[int index] {get; set;} // An indexer
}
One point to note is that interface definition doesn’t contain any modifiers. This is because all
interface methods are public and abstract and therefore implicitly virtual.
An interface can itself inherit from zero, one or more interfaces.

Abstract Class vs. Interface


The choice of whether to design your functionality as an interface or an abstract class (a
MustInherit class in Visual Basic) can sometimes be a difficult one. An abstract class is a class
that cannot be instantiated, but must be inherited from. An abstract class may be fully
implemented, but is more usually partially implemented or not implemented at all, thereby
encapsulating common functionality for inherited classes.
An interface, by contrast, is a totally abstract set of members that can be thought of as defining a
contract for conduct. The implementation of an interface is left completely to the developer.
Both interfaces and abstract classes are useful for component interaction. If a method requires an
interface as an argument, then any object that implements that interface can be used in the
argument. For example:
public void Spin (IWidget widget)
This method could accept any object that implemented IWidget as the widget argument, even
though the implementations of IWidget might be quite different. Abstract classes also allow for
this kind of polymorphism, but with a few caveats:
• Classes may inherit from only one base class, so if you want to use abstract classes to
provide polymorphism to a group of classes, they must all inherit from that class.
• Abstract classes may also provide members that have already been implemented.
Therefore, you can ensure a certain amount of identical functionality with an abstract
class, but cannot with an interface.
Here are some recommendations to help you to decide whether to use an interface or an abstract
class to provide polymorphism for your components.
• If you anticipate creating multiple versions of your component, create an abstract class.
Abstract classes provide a simple and easy way to version your components. By updating
the base class, all inheriting classes are automatically updated with the change.
Interfaces, on the other hand, cannot be changed once created. If a new version of an
interface is required, you must create a whole new interface.
• If the functionality you are creating will be useful across a wide range of disparate
objects, use an interface. Abstract classes should be used primarily for objects that are
closely related, whereas interfaces are best suited for providing common functionality to
unrelated classes.
• If you are designing small, concise bits of functionality, use interfaces. If you are
designing large functional units, use an abstract class.
• If you want to provide common, implemented functionality among all implementations of
your component, use an abstract class. Abstract classes allow you to partially implement
your class, whereas interfaces contain no implementation for any members.

Q - When would you use an abstract base class


A - When you need to enforce a particular interface, but are not able or do not need to define
behavior at the base class level.

Other Differences
Abstract Class Interface
Can have constructor. Can't have constructor.
Can only have abstract methods (implicitly abstract).
May or may not have abstract methods.
Abstract keyword is not allowed.
Methods can have access modifiers. No access modifier is allowed, implicitly public.
Can have fields. Not intended to define fields.
Cannot be directly instantiated. Cannot be directly instantiated.
Can be instantiated via sub-class. Can be instantiated via implementing class.
Can extend a Single class, implement Cannot extends a class, Can extend single or multiple
one or more interfaces. interfaces.
Can have virtual, static methods. virtual, static methods are not allowed.

You might also like