Abstraction is one of the principle 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.
Example:
Consider a real-life scenario of withdrawing money from ATM. The user only
knows that in ATM machine first enter ATM card, then enter the pin code of ATM
card, and then enter the amount which he/she wants to withdraw and at last,
he/she gets their money. The user does not know about the inner mechanism of
the ATM or the implementation of withdrawing money etc. The user just simply
know how to operate the ATM machine, this is called abstraction.
In C# programming, we apply the same meaning
of abstraction by making classes not associated with any specific instance.
Abstraction is done when we need to
only inherit from a certain class, but do not need to instantiate objects of
that class. In such a 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 the abstract keyword with some of its methods (not all) is known as an
Abstract Base Class.
3. Class
having the Abstract keyword with all of its methods is known as pure Abstract
Base Class.
4. The
method of the abstract class that has no implementation is known as
"operation". It can be defined as an abstract void method ();
5. An
abstract class holds the methods but the actual implementation of those methods
is made in derived class.
Consider a classic “shape” example,
perhaps used in a computer-aided design system or game simulation. The base
type is “shape” and each shape has a color, size and so on. From this, specific
types of shapes are derived(inherited)-circle, square, triangle and so on –
each of which may have additional characteristics and behaviours. For example,
certain shapes can be flipped. Some behaviours may be different, such as when you
want to calculate the area of a square.
Example:
// C# program to calculate the
area
// of a square using the concept of
// data abstraction
using System;
namespace Demoabstraction {
// abstract class
abstract class Shape {
// abstract method
public abstract int area();
}
// square class inheriting
// the Shape class
class Square : Shape {
// private data member
private int side;
// method of square class
public Square(int x = 0)
{
side = x;
}
// overriding of the abstract method of Shape
// class using the override keyword
public override int area()
{
Console.Write("Area of Square: ");
return (side * side);
}
}
// Driver Class
class GFG {
// Main Method
static void Main(string[] args)
{
// creating reference of Shape class
// which refer to Square class instance
Shape sh = new Square(4);
// calling the method
double result = sh.area();
Console.Write("{0}", result);
}
}
}
Output:
Area
of Square: 16
.
Advantages of Abstraction
• It
reduces the complexity of viewing the things.
• Avoids
code duplication and increases re usability.
• Helps
to increase security of an application or program as only important details are
provided to the user.
Other principle of OOPs is
What is Encapsulation in C#
What is Inheritance in C#
What is Encapsulation in C#
Other principle of OOPs is
What is Encapsulation in C#
What is Inheritance in C#
What is Encapsulation in C#
Nice
ReplyDelete