Abstraction in C#
In this
article i have explained what is abstraction and where its used in programming
with an example.
Abstraction is an
one of the key concepts of oops. Its is used to expose the necessary
information and encapsulate the unnecessary things.This is achieved using
access modifiers like private, protected.
Key Note :
1.Abstract class
always act as base class.
2.Abstract class
should contains at least one abstract method.
3.It contains also
concrete methods, virtual methods, properties, variable declaration.
4.Abstract class
feature we have to inherit to the child class, Here we have overrides the
abstract method.
Example :
I have a take process of mobile manufacture and development.Initially, a
mobile have basic features and functionality.It will be modified/Enhanced based
on companies.
See example:
Basic
Mobile Feature : MobileFeatures
public abstract
class MobileFeatures
{
public abstract
void Display();
public abstract
void Speaker();
public virtual
string Wifi()
{
return "Wifi - Basic Featues";
}
public virtual
string Camera()
{
return "Camera - Basic Featues";
}
}
One mobile
company(Samsung) is develop mobile inherit from Basic mobile feature. And its
default implementation of Wifi and Camera functionality.We want to modify the
those functionality,to overrides to derived class otherwise it working as
default.
Override the Basic Feature : SamsungGalaxyS6
public class SamsungGalaxyS6 : MobileFeatures
{
static
SamsungGalaxyS6()
{
}
public override void
Display()
{
Console.WriteLine("Display - Integrate functionality");
}
public override void Speaker()
{
Console.WriteLine("Speaker - Integrate functionality");
}
public override string Camera()
{
return
"Camera - Modified Features";
}
}
Access mobile functionality
Bellow , To create a instance of the sub class and call the methods.
class Program
{
static void Main(string[]
args)
{
SamsungGalaxyS6 objAbs = new SamsungGalaxyS6();
objAbs.Camera();
objAbs.Display();
objAbs.Speaker();
objAbs.Wifi();
}
}
No comments:
Post a Comment