0
0
C Sharp (C#)programming~5 mins

When to use abstract vs concrete in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Abstract and concrete classes help organize code by defining what things must do and what things actually do. Abstract classes set rules, while concrete classes do the real work.

When you want to define a general idea but leave details for others to fill in.
When you have common code shared by many classes but some parts differ.
When you want to prevent creating objects directly from a class that is incomplete.
When you want to create objects that do specific tasks based on a shared plan.
When you want to enforce certain methods to be implemented by all subclasses.
Syntax
C Sharp (C#)
abstract class Animal
{
    public abstract void MakeSound();
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }
}

An abstract class cannot be instantiated directly.

A concrete class can be instantiated and must implement all abstract members.

Examples
Abstract class Vehicle defines a method without body. Concrete class Car provides the actual method.
C Sharp (C#)
abstract class Vehicle
{
    public abstract void StartEngine();
}

class Car : Vehicle
{
    public override void StartEngine()
    {
        Console.WriteLine("Car engine started.");
    }
}
Abstract Shape defines a method to get area. Circle implements it with its own formula.
C Sharp (C#)
abstract class Shape
{
    public abstract double GetArea();
}

class Circle : Shape
{
    private double radius;
    public Circle(double radius) { this.radius = radius; }
    public override double GetArea() { return Math.PI * radius * radius; }
}
Concrete class Calculator has a fully implemented method and can be used directly.
C Sharp (C#)
class Calculator
{
    public int Add(int a, int b) { return a + b; }
}
Sample Program

This program shows an abstract class Animal with an abstract method MakeSound. Concrete classes Dog and Cat implement this method. You cannot create an Animal directly, but you can create Dog and Cat objects and call their sounds.

C Sharp (C#)
using System;

abstract class Animal
{
    public abstract void MakeSound();
}

class Dog : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Woof!");
    }
}

class Cat : Animal
{
    public override void MakeSound()
    {
        Console.WriteLine("Meow!");
    }
}

class Program
{
    static void Main()
    {
        // Animal animal = new Animal(); // Error: Cannot create instance of abstract class

        Dog dog = new Dog();
        Cat cat = new Cat();

        Console.WriteLine("Dog says:");
        dog.MakeSound();

        Console.WriteLine("Cat says:");
        cat.MakeSound();
    }
}
OutputSuccess
Important Notes

Time complexity: Using abstract classes does not affect runtime speed directly; it organizes code structure.

Space complexity: Abstract classes do not add extra memory overhead compared to concrete classes.

Common mistake: Trying to create an instance of an abstract class causes a compile error.

Use abstract classes when you want to force subclasses to implement certain methods. Use concrete classes when you want to create objects that do actual work.

Summary

Abstract classes define what must be done but not how.

Concrete classes provide the actual working code.

Use abstract classes to create a common plan and concrete classes to build specific things.