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 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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace @abstract
{
    class Program
    {
        abstract class Pay // Abstract class
        {
            protected int _basicpay = 20000;
            protected int _houserent = 15000;
            protected int _Tax = -500;
            protected int _NetPay = -500;
            public abstract int gradtwo { get; }
            public abstract int gradone { get; }
        }

        class Netpay : Pay
        {
            public void CalculatePay()
            {
                _NetPay = _basicpay + _houserent + _Tax;
            }

            public override int gradtwo // Overriding property
            {
                get
                {
                    return _NetPay;
                }
            }

            public override int gradone // Overriding property
            {
                get
                {
                    return _NetPay = _NetPay + _NetPay * 10 / 100;
                }
            }
        }

        static void Main(string[] args)
        {
            Netpay o = new Netpay();
            o.CalculatePay();
            Console.WriteLine("Officer Grad II pay = {0} \nOfficer Grad I pay = {1}", o.gradtwo, o.gradone);
            Console.ReadKey();
        }
    }
}

Output

Output

Dear reader, I need your extra concentration on this.

Step 1

abstract class Pay   // Abstract class
{
    protected int _basicpay = 20000;
    protected int _houserent = 15000;
    protected int _Tax = -500;
    protected int _NetPay = -500;

    public abstract int gradtwo { get; }
    public abstract int gradone { get; }
}

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

class Netpay : Pay
{
    public void CalculatePay()
    {
        _NetPay = _basicpay + _houserent + _Tax;
    }

    public override int gradtwo // Overriding property
    {
        get
        {
            return _NetPay;
        }
    }

    public override int gradone // Overriding property
    {
        get
        {
            return _NetPay = _NetPay + _NetPay * 10 / 100;
        }
    }
}

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

static void Main(string[] args)
{
    Netpay o = new Netpay();
    o.CalculatePay();
    Console.WriteLine("Officer Grad II pay = {0} \nOfficer Grad I pay = {1}", o.gradtwo, o.gradone);
    Console.ReadKey();
}

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.


Similar Articles