Illustration of Abstract Classes of C# (C Sharp) with Examples

In your C# code, you might have used inheritance. Did you ever get a situation where in you have to define a class which can only take part in inheritance and it cannot be instantiated. If so, how can you accomplish it? You can accomplish it using abstract classes. This article will help you in understanding abstract classes and abstract methods.




Abstract Classes:

General characteristics and restrictions of abstract classes are mentioned below:

• A class becomes an abstract class if you include the keyword abstract in the class declaration
• You cannot instantiate an abstract class
• Abstract classes are intended for inheritance
• You cannot include sealed keyword in an abstract class declaration
• Abstract class should not be associated with the keyword static
• Abstract class can inherit from another class
• Abstract class can implement one or more interfaces

Here is a simple example for abstract class:

abstract class car {
public virtual void displayMsg() {
Console.WriteLine(“You are dealing with a Car”);
}
}
class indica:car { }
class Toyota:car { }
class alto:car { }
class testClass {
public static void Main() {
alto obj = new alto();
obj.displayMsg();
}
}

Output of the code will be:
You are dealing with a Car

If you included the following line of code inside Main method,
Car obj1 = new Car();

Then you will end up in error since abstract classes cannot be instantiated.

Abstract Methods in Abstract Class:

In the above example, your abstract class included a virtual method called displayMsg. Virtual means that the method has to be overridden. However it doesn’t force you to override. Hence in the above example, none of the derived classes have overridden this method. It is legal and all methods are allowed to access it. In the Main method of testClass, you created an instance of alto class and called the displayMsg. This executes the base class method.

In the above example, you will get the same output “You are dealing with a Car” for all cars. What if your user doesn’t agree with it and he wants specialized message for each car. Then you can modify the code as shown below:

abstract class car {
public virtual void displayMsg() {
Console.WriteLine(“You are dealing with a Car”);
}
}
class indica:car {
public override void displayMsg() {
Console.WriteLine(“You are dealing with Indica Car”);
}
}
class Toyota:car {
public override void displayMsg() {
Console.WriteLine(“You are dealing with Toyota Car”);
}
}
class alto:car {
public override void displayMsg() {
Console.WriteLine(“You are dealing with Alto Car”);
}
}
class testClass {
public static void Main() {
indica indObj = new indica();
indObj.displayMsg();
Toyota toyObj = new Toyota();
toyObj.displayMsg();
alto obj = new alto();
obj.displayMsg();
}
}

Output of this code will be:

You are dealing with Indica Car
You are dealing with Toyota Car
You are dealing with Alto Car

You have got the desired output. But still there is a problem with this code. Virtual methods may or may not be overridden in derived class. Since you know that User requires specialized output, you have overridden it. What if at a later point of time, a new car called swift gets introduced and it is implemented by a different developer, then the developer might code it as:

class swift : car { }
class testClass {
public static void Main() {
swift swiftObj = new swift();
swiftObj.displayMsg();
}
}

Output of this code will be:

You are dealing with a Car

This is again undesirable by the User. So the best way to avoid this situation is to force developers to override the method displayMsg. This can be done by associating the keyword abstract with displayMsg instead of virtual. Assume that you have changed the car class as shown below:

abstract class car {
public abstract void displayMsg() {
Console.WriteLine(“You are dealing with a Car”);
}
}

Then you must override this method in all its derived classes. If not, then it will end up in error.

To summarize on abstract methods, here are its basic characteristics and guidelines:

• Abstract classes may or may not define abstract methods
• Abstract methods are defined by including the keyword abstract in the member method declaration before you specify the return type of that method
• Abstract methods will have method signature but no method body
• Only abstract classes can contain abstract methods. Normal classes cannot include abstract methods
• If a class inherits from the abstract class, then it must override all its abstract methods
• Abstract methods are meant to be overridden. Hence they cannot have their access modifier to be private
• Abstract method and its overridden method in the derived class should have the same access modifier
• Abstract methods are internally virtual. Hence you can explicitly mention both abstract and virtual keywords together in the method declaration
• You cannot declare abstract method as static

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| Design Guideline for C# Structs (C Sharp) | Design Patterns – Its Importance and Types | How to Implement Proxy Pattern Using C# (C Sharp)? | How to Implement Singleton Pattern Using C# (C Sharp)? | Illustration of Abstract Classes of C# (C Sharp) with Examples | Illustration of Sealed Classes of C# (C Sharp) with Examples | List of Overloadable Operators in C# (C Sharp) | Usage of [ ] and () Operators in C# (C Sharp) | What is C# (C Sharp) Nested Type? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.