Home > DeveloperSection > Articles > Abstraction in c# with example

Abstraction in c# with example


C# C# 
Ratings:
0 Comment(s)
 3141  View(s)
Rate this:

Abstraction in c# with example

In object-oriented software, complexity is managed by using abstraction. Abstraction is a process that involves identifying the critical behavior of an object and eliminating irrelevant and complex denials. Abstraction is a process of identifying the relevant qualities and behaviors an object should possess.

Example-A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, USB ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works.  You just need to know how to operate the laptop by switching it on.

Note- When derived class inherited with abstract class; derived class must be override abstract class methods.

Example of Abstraction:

using System;

using System.Collections.Generic;

using System.Linq;

 

namespace abstarction

{

    public abstract class university

    {

        public abstract void BTech();

        public abstract void MBA();

    }

 

    public class GBTU : university

    {

        public override void BTech()

        {

            Console.WriteLine("GBTU BTech Fee 50000/-");

        }

 

        public override void MBA()

        {

            Console.WriteLine("GBTU MBA Fee 100000/-");

        }

    }

 

    public class MTU : university

    {

        public override void BTech()

        {

            Console.WriteLine("MTU BTech Fee 40000/-");

        }

 

        public override void MBA()

        {

            Console.WriteLine("MTU MBA Fee 800000/-");

        }

    }

 

 

    class Program

    {

        static void Main(string[] args)

        {

            GBTU g = new GBTU();

            g.BTech();

            g.MBA();

            MTU m = new MTU();

            m.BTech();

            m.MBA();

            Console.ReadLine();

        }

    }

}

 

 

 

Output-

GBTU BTech Fee 50000/-

GBTU MBA Fee 100000/-

MTU BTech Fee 40000/-

MTU MBA Fee 800000/-

 

Explanation :-

Ø  From above example we have make one abstract class university and two abstract methods Btech and MBA. GBTU and MTU both are override university course fee.

Ø  University course common for both GBTU and MTU so university method BTech and MBA is abstract.

Ø  GBTU and MTU inherited abstract method so university method must be override here.


Don't want to miss updates? Please click the below button!

Follow MindStick