0% found this document useful (0 votes)
8 views10 pages

Abstract Classes & Methods in C# Explained

Abstract classes in C# are classes that contain abstract methods, which are methods without a body that must be implemented by derived classes. An abstract class can have both abstract and non-abstract methods, and cannot be instantiated directly. Child classes must implement all abstract methods to access the non-abstract methods of the parent abstract class.

Uploaded by

alihamzyazmanpak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views10 pages

Abstract Classes & Methods in C# Explained

Abstract classes in C# are classes that contain abstract methods, which are methods without a body that must be implemented by derived classes. An abstract class can have both abstract and non-abstract methods, and cannot be instantiated directly. Child classes must implement all abstract methods to access the non-abstract methods of the parent abstract class.

Uploaded by

alihamzyazmanpak
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Abstract Class and Abstract Methods in C#

Abstract Classes and Abstract Methods in C# with Examples

In this article, I am going to discuss Abstract Classes and Abstract Methods in C# with Examples. Please
read our previous article, where we discussed Generalization and Specialization in C# with Examples. In
this article, we will discuss what are abstract classes and what is the use of abstract classes in our
application. Let us first understand what are abstract methods and abstract classes in C#.

What are Abstract Methods in C#?

A method without the body is known as Abstract Method, what the method contains is only the
declaration of the method. That means the abstract method contains only the declaration, no
implementation. The following method is a non-abstract method as this method contains a body.
public void Add(int num1, int num2)
{
}

But without writing the method body, if we end the method with a semicolon as follows, then it is called
an Abstract Method.
public void Add(int num1, int num2);

But, remember if you want to make any method an abstract method, then you should explicitly use the
abstract modifier as follows. And once you use the abstract modifier, automatically the method is going
to be called an abstract method.
public abstract void Add(int num1, int num2);

Then what are abstract classes?

What are Abstract Classes in C#?

A class under which we define abstract methods is known as an abstract class. As per object-oriented
programming, we need to define a method inside a class. And, we cannot define the abstract methods
directly anywhere, we need to define the abstract method inside an abstract class only. Suppose, we
need to write the above Add abstract method inside a class called Calculator, then that class needs to be
declared using the abstract modifier as follows.
public abstract class Calculator
{
public abstract void Add(int num1, int num2);
}
So, when a class contains any abstract methods, then it must and should be declared using the abstract
modifier and when a class is created using an abstract modifier then it is called an Abstract class in C#.
So, this is how exactly we define an abstract class and abstract methods in C#.

Generally, when we define an abstract class we have a doubt, without a method body what will be the
use of that method? Let us understand this.

What is the use of the Abstract Method in C#?


If a method is declared as abstract under any class, then the child class of that abstract class is
responsible for implementing the abstract method without fail.

In inheritance, we see that the Parent class provides some properties to the Child class for consumption.
Here also inheritance comes into the picture, but the point that you need to remember is that the
Parent class is an abstract class, and he will not be providing any properties to the Child class for
consumption, rather it imposes some restrictions on the Child classes. And children or Child classes have
to be followed or fulfill those restrictions. And that is the basic idea of abstract class in C#. We will come
to this point later.

Note: Every abstract method declared within an abstract class must and should be implemented by the
Child classes without fail else we will get compile time error.

Is Abstract Class Containing Only Abstract Methods in C#?

No, don’t think that an abstract class can contain only abstract methods. It can also contain non-abstract
methods. The point that you need to remember is, that if a class is non-abstract then it contains only
non-abstract methods but if a class is abstract then it contains both abstract and non-abstract methods
in C#.

Who will Provide Implementation of Abstract Methods in C#?

The Answer is Child Class. If you have a child class of an abstract class, then it is the responsibility of the
child class to provide the implementation for all the abstract methods of the parent class. You cannot
escape. Every method should be implemented. If you implement all the abstract methods, then only you
can consume the non-abstract method of the Parent class.

Generally, what we saw in Inheritance is that the child class can directly consume the members of the
parent class. But here this is not possible. You cannot consume directly. The property is under
restrictions. Until and unless the restrictions are fulfilled by the child class, the child class cannot
consume the members of the parent class.

So, the point that you need to remember is, that in the child class, you need to implement each and
every abstract method of the parent class, and then only you can consume the non-abstract methods of
the parent class.

Let us compare this with one real-time example. Suppose, the father promised his son, that if you bring
90% of the mark in the annual exam, then he will be rewarded with a laptop. So, the laptop is going to
be given to you only if you bring 90% in the annual exam. Now, if the son wants to get the laptop, then
the son has to fulfill the requirement set by his father. What is the requirement, the requirement is
achieving 90% of the mark. Once the son fulfills the requirement i.e. once the son achieves 90% marks in
the annual exam, then the laptop is given to him until then he will not get the laptop.

This is exactly the same in the case of an abstract class. Abstract class contains both abstract and non-
abstract methods. You can consider the abstract method as Marks obtained on the annual exam and the
non-abstract method as the laptop. So, if you want to get the laptop (i.e. to consume non-abstract
method), you need to fulfill the requirements i.e. get 90% marks in the annual exam (i.e. implement all
the abstract methods).
Note: To define a method as abstract or class as abstract, we require to use the abstract keyword on
them.

Example to Understand Abstract Class and Abstract Methods in C#:

Let us understand Abstract Class and Abstract Methods in C# with an Example. Please have a look at the
following class. This is going to be our parent abstract class. In this class, we have defined two non-
abstract methods i.e. Add and Sum, and two abstract methods i.e. Mul and Div. Further, if you notice we
create the class AbsParent using the abstract keyword as this class contains two abstract methods.

public abstract class AbsParent

public void Add(int x, int y)

[Link]($"Addition of {x} and {y} is : {x + y}");

public void Sub(int x, int y)

[Link]($"Subtraction of {x} and {y} is : {x - y}");

public abstract void Mul(int x, int y);

public abstract void Div(int x, int y);

Can we create an instance of an abstract class in C#?

No. We cannot create an instance of an abstract class. Whether the abstract class contains any abstract
methods or not, it is not possible to create an instance of the abstract class. If you try, you will get a
compile-time error as shown in the below image.
As you can see in the above image, it is clearly saying that you cannot create an instance of an abstract
class and this makes sense. This is because, if it will allow us to create an instance of the abstract class,
then using the instance you can call the abstract class abstract methods which do not have a body and
this is the reason why it is not allowing us to create an instance of the abstract class in C#.

Currently, the abstract class does not have any static members. If there are any static members, you can
call them directly using the class name. But, for calling non-static members we need an instance. Then
who can consume the above members? The answer is child class.

Suppose, there is a child class for the above AbsParent class, then the child class has to implement the
Mul and Div abstract methods before consuming the Add and Sub method. Please observe the following
code. Here, we have created the AbsChild class inheriting from the AbsParent class. And here we have
not implemented the two abstract methods. So, it is giving us a compile-time error.

Here, we are getting two errors. One for not implementing the parent class Div method and another
error for not implementing the Parent class Mul method. That means it is mandatory for the child class
to provide the implementation for all the abstract methods of the parent class.

Why Abstract Class Cannot Be Instantiated in C#?

Because it is not a fully implemented class as its abstract methods cannot be executed. If the compiler
allows us to create the object for an abstract class, we can invoke the abstract method using that object
which cannot be executed by CLR at runtime. Hence to restrict calling abstract methods, the compiler
does not allow us to instantiate an abstract class.

Now, let us implement the two abstract methods inside the child class. How to implement the abstract
methods means using the override modifier as follows.

public class AbsChild : AbsParent

public override void Mul(int x, int y)

[Link]($"Multiplication of {x} and {y} is : {x * y}");

public override void Div(int x, int y)


{

[Link]($"Division of {x} and {y} is : {x / y}");

Now, you can see there is no more compile-time error. Now, the child class fulfills the requirements of
the parent class by implementing the abstract methods, and hence the child class can now consume the
non-abstract methods of the parent class. So, you can now, create an instance of the Child class and
consume all the members as follows.

The complete example code is given below.

using System;

namespace AbstractClassesAndMethods

class Program

static void Main(string[] args)

AbsChild absChild = new AbsChild();

[Link](10, 5);

[Link](10, 5);

[Link](10, 5);

[Link](10, 2);

[Link]();
}

public abstract class AbsParent

public void Add(int x, int y)

[Link]($"Addition of {x} and {y} is : {x + y}");

public void Sub(int x, int y)

[Link]($"Subtraction of {x} and {y} is : {x - y}");

public abstract void Mul(int x, int y);

public abstract void Div(int x, int y);

public class AbsChild : AbsParent

public override void Mul(int x, int y)

[Link]($"Multiplication of {x} and {y} is : {x * y}");

public override void Div(int x, int y)

[Link]($"Division of {x} and {y} is : {x / y}");

Output:
Can we Create a Reference for the Abstract Class in C#?

Yes, we can create a reference for the abstract class in C#. But we cannot create an instance of an
abstract class in C#. For a better understanding, please have a look at the below image. Here, we
created an instance of child class i.e. AbsChild, and then we created a reference of abstract class i.e.
AbsParent which is holding the child class instance and then using the reference we can also access the
members.

The important point that you need to remember is, parent class references even if created by using child
class instances cannot call child class methods provided the methods are purely defined in the child
class. Overridden methods are not pure child class methods. If a method is overridden in the child class
means it has taken permission from the parent class. So, the parent is completely aware of that method.
So, the parent class references can also call the child class overridden members but cannot call the pure
child class members.

For a better understanding of this concept, please have a look at the below example. Here, the child
class overrides the parent class members as well as we defined a pure child class method i.e. Mod in the
child class. As this method is purely defined in the child class, we cannot call this method using the
Parent class reference variable. Using the parent class reference variable, we can call the parent class
non-abstract methods, child class overridden methods but not the pure child class methods.

using System;
namespace AbstractClassesAndMethods

class Program

static void Main(string[] args)

//Creating Child class instance

AbsChild absChild = new AbsChild();

//Creating abstract class reference pointing to child class object

AbsParent absParent = absChild;

//Accessing methods using reference

[Link](10, 5);

[Link](10, 5);

[Link](10, 5);

[Link](10, 2);

//You cannot call the Mod method using Parent reference as it is a pure child class method

//[Link](100, 35);

[Link]();

public abstract class AbsParent

public void Add(int x, int y)

[Link]($"Addition of {x} and {y} is : {x + y}");

public void Sub(int x, int y)

[Link]($"Subtraction of {x} and {y} is : {x - y}");


}

public abstract void Mul(int x, int y);

public abstract void Div(int x, int y);

public class AbsChild : AbsParent

public override void Mul(int x, int y)

[Link]($"Multiplication of {x} and {y} is : {x * y}");

public override void Div(int x, int y)

[Link]($"Division of {x} and {y} is : {x / y}");

public void Mod(int x, int y)

[Link]($"Modulos of {x} and {y} is : {x % y}");

Output:

Summary of Abstract Class and Abstract Methods in C#

1. A method that does not have a body is called an abstract method and the class that is declared
by using the keyword abstract is called an abstract class. If a class contains an abstract method,
then it must be declared as abstract.
2. An abstract class can contain both abstract and non-abstract methods. If a child class of an
abstract class wants to consume any non-abstract methods of its parent, should implement all
abstract methods of its parent.

3. An abstract class is never usable to itself because we cannot create the object of an abstract
class. The members of an abstract class can be consumed only by the child class of the abstract
class.

You might also like