Learn Object Oriented Programming Using C#: Part 8

Before reading this article, please go through the following articles:
  1. Object-Oriented Programming Using C#: Part 1
  2. Object-Oriented Programming Using C#: Part 2
  3. Object-Oriented Programming Using C#: Part 3
  4. Object-Oriented Programming Using C#: Part 4
  5. Object-Oriented Programming Using C#: Part 5
  6. Object-Oriented Programming Using C#: Part 6
  7. 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 are 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; show only the details that matter to the user.
 
Example
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace @abstract {  
  7.     class Program {  
  8.   
  9.         abstract class pay // Abstract class  
  10.         {  
  11.             protected int _basicpay = 20000;  
  12.             protected int _houserent = 15000;  
  13.             protected int _Tax = -500;  
  14.             protected int _NetPay = -500;  
  15.             public abstract int gradtwo { get; }  
  16.             public abstract int gradone { get; }  
  17.         }  
  18.   
  19.         class Netpay: pay {  
  20.             public void CalculatePay() {  
  21.                 _NetPay = _basicpay + _houserent + _Tax;  
  22.             }  
  23.   
  24.             public override int gradtwo // overriding property  
  25.             {  
  26.                 get {  
  27.                     return _NetPay;  
  28.                 }  
  29.             }  
  30.   
  31.             public override int gradone // overriding property  
  32.             {  
  33.                 get {  
  34.                     return _NetPay = _NetPay + _NetPay * 10 / 100;  
  35.                 }  
  36.             }  
  37.         }  
  38.         static void Main(string[] args) {  
  39.             Netpay o = new Netpay();  
  40.             o.CalculatePay();  
  41.             Console.WriteLine("Officer Grad II pay = {0} \nOfficer Grad I pay = {1}", o.gradtwo, o.gradone);  
  42.             Console.ReadKey();  
  43.         }  
  44.     }  
Output
 
Image.jpg
 
Dear reader, I need your extra concentration for this.
 
Step 1
  1. abstract class pay   // Abstract class  
  2. {  
  3.     protected int _basicpay = 20000;  
  4.     protected int _houserent = 15000;  
  5.     protected int _Tax = -500;  
  6.     protected int _NetPay = -500;        
  7.     public abstract int gradtwo    { get; }  
  8.     public abstract int gradone { get; }  
  9.  } 
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
  1. class Netpay: pay {  
  2.     public void CalculatePay() {  
  3.         _NetPay = _basicpay + _houserent + _Tax;  
  4.     }  
  5.   
  6.     public override int gradtwo // overriding property  
  7.     {  
  8.         get {  
  9.             return _NetPay;  
  10.         }  
  11.     }  
  12.   
  13.     public override int gradone // overriding property  
  14.     {  
  15.         get {  
  16.             return _NetPay = _NetPay + _NetPay * 10 / 100;  
  17.         }  
  18.     }  
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 "gradone" and "gradtwo" that will return the values of "_NetPay".
 
Step 3
  1. static void Main(string[] args)  
  2. {  
  3.      Netpay o = new Netpay();  
  4.      o.CalculatePay();  
  5.      Console.WriteLine("Officer Grad II pay = {0} \nOfficer Grad I pay = {1}", o.gradtwo, o.gradone);  
  6.      Console.ReadKey();  
  7.  } 
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 necessary to be understood.


Similar Articles