Understand Abstract Class and Abstract Method using example
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.
abstract class absCalculator ...code starts here }
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); } }
Hey friends if you have any doubt or query kindly let me know through your comments.
By Anusha on 2015-06-03very easy to undertsand with simple examples.. Oops concepts are explained clearly . This is what every developers need to know . Thanks for the post
By Vinayagamurugan on 2014-11-25This 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-25So actually when we have to use abstract class in a live project give some examples.
By Ferose on 2014-06-19thanks 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 defined
By Saurabh Bansal on 2014-06-10Thats gr8
By Waqar on 2014-05-06Good explanation
By Waqar on 2014-05-06Above is a good Explanation Can you teach me what is aggregation by value /by refrence/by composition
By Badharinadh on 2014-04-15HI gurunatha, absCalculate objabCal = new clsCalculate(); why we are creating obj like that in abstract class.pls send description to my email
By Sharika on 2014-03-25Thank you for this code.Can you explain the exact use of abstract class..
By Raja on 2013-10-09nice to understand in this program.
By Ankit on 2013-09-16what's the mean of composition in C#?
Add a Comment
Thank you, for commenting !!
Your comment is under moderation...
Keep reading other blogs