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 reader's, 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 abstraction; show 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


Image.jpg


Dear reader, I need your extra concentration for 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 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 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 overriden the two properties "gradone" and "gradtwo" 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 necessary to be understood.
 

Similar Articles