Before reading this article, please go through the following articles.
- Object-Oriented Programming Using C#: Part 1
- Object-Oriented Programming Using C#: Part 2
- Object-Oriented Programming Using C#: Part 3
- Object-Oriented Programming Using C#: Part 4
- Object-Oriented Programming Using C#: Part 5
- Object-Oriented Programming Using C#: Part 6
- Object-Oriented Programming Using C#: Part 7
Abstract Methods
Dear readers, this article is the fourth and last pillar of OOP. It's confusing for the beginners of OOP. So we provide an example in very simple words.
"Abstraction is used to manage complexity. No objects of an abstract class can be created. An abstract class is used for inheritance."
For Example
When we drive a car we often need to change the gears of the vehicle but we are otherwise not concerned about the inner details of the vehicle engine. What matters to us is that we must shift gears, that's it. This is an abstraction; that shows only the details that matter to the user.
Example
Output
![Output]()
Dear reader, I need your extra concentration on this.
Step 1
I have defined one abstract class "pay" with a protected variable that can only be accessed by the same class or in a derived class. These member variables are initiated with values.
Step 2
In this step, we have defined the class "Netpay" derived from the abstract base class "pay".
In that class, we have defined the "CalculatePay" method as having public access modifiers to calculate the pay of the employee. During the pay calculation, we used a protected variable from the base class. Here we have overridden the two properties "grade" and "grade two" that will return the values of "_NetPay".
Step 3
In the void main session, we have created the object of the "Netpay" class. Using the object we call the "CalculatePay" method that will do the calculation of the pay.
So the user is only concerned with the pay of the employee and its output. How this pay is calculated is not necessarily to be understood.