Understand Abstract Class and Abstract Method using example
- By Gurunatha Dogi in C#
- Apr 19th, 2013
- 54934
- 11
About Abstract Class
Abstract class in csharp is defined using "abstract" keyword
An abstract class may contain abstract methods and accessors (properties).
Non abstract methods or properties should provide actual implementation in an abstract class.
Abstract classes that cannot be instantiated mean we cannot able to create an object of an abstract class but abstract class can be implemented to child classes like a common base class. An abstract class can be partially implemented or not at all implemented.
Syntax
abstract class absCalculator ...code starts here }
Abstract Method
In an abstract class a method which has a keyword "abstract" and doesn't provide any implementation is called abstract method.The implementation logic of abstract methods is provided by the child classes or derived classes.Child classes use keyword "override" with same method name (as abstract method name) to provide further implementation of abstract methods.
Non Abstract Method
In an abstract class a method which doesn't have a keyword "abstract" and provide any implementation is called non abstract method.
Why Abstract Class
Abstract class enforces derived classes to provide all implementation logic for abstract methods or properties.
Use of an Abstract Class
Use abstract class when you want to create a common base class for a family of types and with some implementation
Subclass only a base class in a hierarchy to which the class logically belongs.
Below a simple example in c# which illustrates abstract class with abstract methods and non abstract methods.
First step lets create an abstract class "absCalculate" having abstract methods and non abstract methods.
abstract class absCalculate //A Non abstract method public int Addition(int Num1, int Num2) return Num1 + Num2; } //An abstract method, to be overridden in derived class public abstract int Multiplication(int Num1, int Num2); }
Now next step lets create a class "clsCalculate" and implement it with abstract class "absCalculate".
class clsCalculate:absCalculate ...code }
And now lets build this application.
When we build this application we got the following error because we have not provided implementation of an abstract method as you can see it in above snapshot.
Now lets give implementation for an abstract method using override keyword implementing the abstract method as you can see in our below code.
class clsCalculate:absCalculate //using override keyword implementing the abstract method public override int Multiplication(int Num1, int Num2) return Num1 * Num2; } }
Finally lets create an object of class "clsCalculate" in our main program of an console application and see the output.
class Program static void Main(string[] args) Console.WriteLine("Please Enter First Number"); int num1 = Convert.ToInt16(Console.ReadLine()); Console.WriteLine("Please Enter Second Number"); int num2 = Convert.ToInt16(Console.ReadLine()); absCalculate objabCal = new clsCalculate(); int add = objabCal.Addition(num1, num2); int multiplied = objabCal.Multiplication(num1, num2); Console.WriteLine("Added Number is : 0}, Multiplied Number is : 1}", add, multiplied); } }
Output
Hey friends if you have any doubt or query kindly let me know through your comments.
Gurunatha Dogi
Gurunatha Dogi is a software engineer by profession and founder of Onlinebuff.com, Onlinebuff is a tech blog which covers topics on .NET Fundamentals, Csharp, Asp.Net, PHP, MYSQL, SQL Server and lots more..... read more
Comments
By Anusha on 2015-06-03
very easy to undertsand with simple examples.. Oops concepts are explained clearly . This is what every developers need to know . Thanks for the postBy Vinayagamurugan on 2014-11-25
This is very easy to understand c# concepts with examples comparing to other sites.. I read OOPL concepts using c# can be learned clearly with the help of Onlinebuff.com. Thanks a lot.By Yogesh Chandra Upreti on 2014-08-25
So actually when we have to use abstract class in a live project give some examples.By Ferose on 2014-06-19
thanks for the post Please clear the following doubt If i have two abstract methods in the abstract class and i had derived the abstract class. I wold like to know whether two abstract method definition part has to be included in the derived class or any one abstract method can be definedBy Saurabh Bansal on 2014-06-10
Thats gr8By Waqar on 2014-05-06
Good explanationBy Waqar on 2014-05-06
Above is a good Explanation Can you teach me what is aggregation by value /by refrence/by compositionBy Badharinadh on 2014-04-15
HI gurunatha, absCalculate objabCal = new clsCalculate(); why we are creating obj like that in abstract class.pls send description to my emailBy Sharika on 2014-03-25
Thank you for this code.Can you explain the exact use of abstract class..By Raja on 2013-10-09
nice to understand in this program.By Ankit on 2013-09-16
what's the mean of composition in C#?Add a Comment

- By Gurunatha Dogi
- Apr 16th, 2013
- 26226
- 11
What are Delegates in C# - Step By Step Creating and Using the Delegate

- By Gurunatha Dogi
- Nov 19th, 2012
- 32019
- 11