You are on page 1of 11

Abstraction in C# using an example

Definition

Abstraction is one of the principles of object oriented programming. It is


used to display only necessary and essential features of an object to
outside the world. Means displaying what is necessary and encapsulate
the unnecessary things to outside the world. Hiding can be achieved by
using "private" access modifiers.

Note - Outside the world means when we use reference of object then it
will show only necessary methods and properties and hide methods which
are not necessary.

Implementation of Abstraction

To implement abstraction let's take an example of a car. We knows a car,


Car is made of name of car, colour of car, steering, gear, rear view mirror,
brakes, silencer, exhaust system, diesel engine, car battery, car engine
and other internal machine details etc.

Now lets think in terms of Car rider or a person who is riding a car. So to
drive a car what a car rider should know from above category before he
starts a car driving.

Necessary things means compulsory to know before starting a car

1. Name of Car
2. Color of Car
3. Steering
4. Rear View Mirror
5. Brakes
6. Gear
Unnecessary things means not that compulsory to know for a Car rider

1. Internal Details of a Car


2. Car Engine
3. Diesal Engine
4. Exhaust System
5. Silencer

Now above same thing let me put in the coding style using C#

public class Car


private string _nameofcar = "My Car";

private string _colorofcar = "Red";

public string NameofCar

set

_nameofcar = value;

get
return _nameofcar;

public string ColorofCar


set

_colorofcar = value;

}
get
return _colorofcar;

}
}

public void Steering()

Console.WriteLine("Streering of Car");

public void RearViewMirror()


Console.WriteLine("RearViewMirror of Car");

public void Brakes()


Console.WriteLine("Brakes of Car");

}
public void Gear()
Console.WriteLine("Gear of Car");

}
private void InternalDetailsofCar()
Console.WriteLine("InternalDetailsofCar of Car");

private void CarEngine()


Console.WriteLine("CarEngine of Car");
}
private void DiesalEngine()
Console.WriteLine("DiesalEngine of Car");

}
private void ExhaustSystem()

Console.WriteLine("ExhaustSystem of Car");

private void Silencer()


Console.WriteLine("Silencer of Car");

}
As you can see from above code that necessary methods and properties
exposed by using "public" access modifier and unnecessary methods and
properties (not compulsory) hidden by using "private" access modifier.

As you see to achieve abstraction we used access modifier "public" to


expose some methods and properties to outside the class or world.

As you see to achieve abstraction we used access modifier "private" to


hide some methods and properties from outside the class or world.
Finally lets check by creating an object of above class "Car" in our main
program of a console application and by using object lets see whether we
are getting all exposed necessary methods and properties.

class Program

static void Main(string[] args)

Car objCar = new Car();

}
}
Conclusion

Successfully we have exposed necessary methods and properties to


outside the world or class. This is how we need to implement abstraction
or we can achieve abstraction in our code or application.

Friends feel free to ask me any queries on abstraction in c#. Thank you

Abstraction

The word abstract means a concept or an idea not associated with any
specific instance.

In programming we apply the same meaning of abstraction by making


classes not associated with any specific instance.

The abstraction is done when we need to only inherit from a certain class,
but not need to instantiate objects of that class. In such case the base
class can be regarded as "Incomplete". Such classes are known as an
"Abstract Base Class".

Abstract Base Class

There are some important points about Abstract Base Class :

1. An Abstract Base class cannot be instantiated; it means the object


of that class cannot be created.
2. Class having abstract keyword and having, abstract keyword with
some of its methods (not all) is known as an Abstract Base Class.

3. Class having Abstract keyword and having abstract keyword with all
of its methods is known as pure Abstract Base Class.

4. The method of abstract class that has no implementation is known


as "operation". It can be defined as abstract void method ();

5. An abstract class holds the methods but the actual implementation


of those methods is made in derived class.

Lets have a look of this code!

abstract class animal


{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
This is the Abstract Base Class, if I make both of its methods abstract then
this class would become a pure Abstract Base Class.

Now we derive a class of 'dog' from the class animal.

abstract class animal


{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
class dog : animal
{
public override void eat() { Console.WriteLine("dog can eat"); }
}

Here you can see we have 2 methods in the Abstract Base Class, the
method eat() has no implementation; that is why it is being declared as
'abstract' while the method sound() has its own body so it is not declared
as 'abstract'.

In the derived class we have the same name method but this method has
it's body.

We are doing abstraction here so that we can access the method of


derived class without any trouble.

Let's have a look!

class program
{
abstract class animal
{
public abstract void eat();
public void sound()
{
Console.WriteLine("dog can sound");
}
}
class dog : animal
{
public override void eat() { Console.WriteLine("dog can eat"); }
}
static void Main(string[] args)
{
dog mydog = new dog();
animal thePet = mydog;
thePet.eat();
mydog.sound();
}
}

Finally we created an Object 'mydog' of class dog, but we didn't


instantiate any object of Abstract Base Class 'animal'.

According to "Ivor Horton" (a programmer of Java) an object can not be


instantiated, but we can declare a variable of the Abstract Class type. If
this statement is true then it could be possible:

animal thePet;

This is an object which is declared as thePet and its data type is the
abstract base class 'animal'.

We can use this Object to store Objects of the subclass.

In the above code we declare an Object 'thePet', of the type animal (the
Abstract Base Class) and simply copied the object of another object (only
the reference is copied as they belong to reference type). Now we can use
object 'thePet' just as object 'mydog'.
The output of this code would be as

dog can eat


dog can sound

Conclusion:

l concludes here by saying that Abstraction is not a difficult job to do, but
you need to be confident while performing abstraction. Our every new
topic covers the all previous topics. In the abstraction the polymorphism is
being covered and performed. The method overriding could be done by
putting the keyword 'new' before that overridden method. Everything is
possible in Programming and there are multiple ways to do a single job.
Abstraction is one of the smart ways to do these kind of tasks.

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 behaviours 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,
speakers 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 andMBA. 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.