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("Abstarct 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("Abstarct method #1");
}
}
class MyClass : MyClass1
{
public override void AbMethod2()
{
Console.WriteLine("Abstarct 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("Abstarct 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("Abstarct method #1 of MyClass1");
}
public override void AbMethod2()
{
Console.WriteLine("Abstarct method #2 of MyClass1");
}
}
class MyClient
{
public static void Main()
{
MyAbs ma1 = new MyClass1();// Polymorphism
ma1.AbMethod1();
ma1.AbMethod2();
}
}


Recommended Free Ebook
Similar Articles