Abstraction in C#
In this article we are going to see, Abstraction in C#.So, What is an Abstraction in C#? Abstraction is one of the key concepts of Object-Oriented Programming which is used to hide the complexity of data from users or consumers. There are several ways to achieve it, like using private access modifier, Interfaces and Abstract classes. For example, in the case of WCF services we provide Interfaces to consumers not an actual class. Because the actual class might have various methods and data which is useless and confusing for the consumer. Following is a simple program to understand it.
Code Snippet:-
public abstract class Animal
{
Code Snippet:-
public abstract class Animal
{
// Open to Consumer or user
public abstract void Eat(string _food);
public void Sounds(string _voice)
{
Console.WriteLine("And sounds like " + _voice);
}
}
public class Lion: Animal
{
public abstract void Eat(string _food);
public void Sounds(string _voice)
{
Console.WriteLine("And sounds like " + _voice);
}
}
public class Lion: Animal
{
public override void Eat(string _food)
{
Console.WriteLine("Lion likes to eat " + _food);
}
}
public class AbstarctionDemo
{
static void Main(string[] args)
{
Animal Obj_Animal = new Lion();
Obj_Animal.Eat("meat.");
Obj_Animal.Sounds("roar.");
Console.ReadLine();
}
}
{
Console.WriteLine("Lion likes to eat " + _food);
}
}
public class AbstarctionDemo
{
static void Main(string[] args)
{
Animal Obj_Animal = new Lion();
Obj_Animal.Eat("meat.");
Obj_Animal.Sounds("roar.");
Console.ReadLine();
}
}
Comments
Post a Comment