Abstraction is about showing what is necessary.
In C# we have abstract classes and abstract methods to achieve abstraction.
- Abstract Class is a restricted class or can say a half-defined class, so we can not create an instance of the class. To use this class it needs to be inherited.
- Abstract Method can only be declared in abstract classes and it does not contain implementation like an Interface method declaration. Method implementation is a must to their derived classes.
- An abstract class can also contain regular methods with abstract ones.
- You cannot create an instance of an abstract class
- An abstract class with all abstract methods will work as an Interface
- An abstract class can not be declared as Sealed
{
public abstract void Sound();
{
//Implementation
}
class Dove: Bird
{
public override void Sound() //abstract method implementation
{
Console.WriteLine("Coo Coo");
}
public override void Sleep() //Virtual method can be overridden and have its own implementation
{
//Implementation
}
}
Abstract classes provide security and hide the complexity of the object so the object shows only necessary details to the client. Abstraction can also be achieved using Interfaces. Kindly refer dedicated topic on Interface to learn more.
No comments:
Post a Comment