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 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: Capsule Medicine
Encapsulation and
abstraction is the advanced mechanism in C# that lets your program to hide
unwanted code within a capsule and shows only essential features of an object.
Encapsulation is used to hide its members from outside class or interface,
whereas abstraction is used to show only essential features.
In C# programming, Encapsulation uses five types of modifier to
encapsulate data. These modifiers are public, private, internal, protected and
protected internal. These all includes different types of characteristics and
makes different types of boundary of code.
Access Modifier
|
Description (who can
access)
|
Private
|
Only members within
the same type. (default for type members)
|
Protected
|
Only derived types
or members of the same type.
|
Internal
|
Only code within the
same assembly. Can also be code external to object as long as it is in the
same assembly. (default for types)
|
Protected internal
|
Either code from
derived type or code in the same assembly. Combination of protected OR
internal.
|
Public
|
Any code. No
inheritance, external type, or external assembly restrictions.
|
public abstract class Car
{
//you cant
declare private access specifier
public abstract void
StartCar();
public void dispay()
{
Console.WriteLine("Concrete Method");
}
}
public class Audi : Car
{
#region Overrides of Car
public override void
StartCar()
{
Console.WriteLine("Audi StartCar");
}
#endregion
}
public class BMW : Car
{
#region Overrides of Car
public override void
StartCar()
{
Console.WriteLine("BMW StartCar");
}
#endregion
}
class Program
{
static void Main(string[] args)
{
Car obj = new BMW();
obj.dispay();
obj.StartCar();
Console.ReadLine();
}
}
Output: Concrete Method
BMW StartCar
0 comments :
Post a Comment