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.