Abstraction in C#

Show only what matters. Hide the engine complexity, show the steering wheel.

60-Second Version: Abstraction = hide complex details. abstract class Car defines WHAT must exist. Child classes define HOW it works.

1. Abstract Class = Incomplete Design

Can't create object from it. Used as base only.

public abstract class Car
{
    public abstract void Start(); // No code here. Child MUST write it
    public void Honk() { Console.WriteLine("Beep"); } // Normal method OK
}
// Car c = new Car(); // Error: Cannot create abstract class

2. Child Must Implement

public class SportsCar : Car
{
    public override void Start() { Console.WriteLine("VROOM!"); } // Required
}

SportsCar s = new SportsCar();
s.Start(); // VROOM!
s.Honk(); // Beep - inherited
Beginner Trap: Forgetting override on abstract method. Compiler error. Abstract = "you MUST override this".

Quick Check ๐Ÿง 

Next: Interfaces - Define CAN-DO abilities. Multiple inheritance the C# way.

Comments on Abstraction (0)

No comments yet. Be the first to share your thoughts!