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

Abstract classes and methods in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Abstract classes let you create a base blueprint that other classes can follow. Abstract methods are like empty promises that child classes must fill in.

When you want to define a common structure for related classes but don't want to create objects of the base class.
When you want to force child classes to provide their own specific behavior for certain actions.
When you have some shared code but also some parts that must be customized by each child class.
Syntax
C Sharp (C#)
abstract class Animal
{
    public abstract void MakeSound();

    public void Eat()
    {
        Console.WriteLine("This animal is eating.");
    }
}

An abstract class cannot be instantiated directly.

Abstract methods have no body and must be implemented by subclasses.

Examples
Vehicle is abstract with an abstract method. Car provides the actual implementation.
C Sharp (C#)
abstract class Vehicle
{
    public abstract void StartEngine();
}

class Car : Vehicle
{
    public override void StartEngine()
    {
        Console.WriteLine("Car engine started.");
    }
}
Shape defines an abstract method GetArea. 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() => Math.PI * radius * radius;
}
Abstract method must be implemented or the code won't compile.
C Sharp (C#)
abstract class EmptyBase
{
    public abstract void DoSomething();
}

// This will cause a compile error if DoSomething is not implemented
class Derived : EmptyBase
{
    public override void DoSomething()
    {
        Console.WriteLine("Doing something.");
    }
}
Sample Program

This program shows an abstract class Animal with an abstract method MakeSound and a normal method Eat. Dog and Cat classes implement MakeSound differently. We create Dog and Cat objects and call their methods.

C Sharp (C#)
using System;

abstract class Animal
{
    public abstract void MakeSound();

    public void Eat()
    {
        Console.WriteLine("This animal is eating.");
    }
}

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 dog = new Dog();
        Animal cat = new Cat();

        dog.Eat();
        dog.MakeSound();

        cat.Eat();
        cat.MakeSound();
    }
}
OutputSuccess
Important Notes

Time complexity: Abstract methods themselves do not affect time complexity; it depends on the implementation in subclasses.

Space complexity: Abstract classes do not add extra memory overhead beyond normal classes.

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

Use abstract classes when you want to share code and force certain methods to be implemented by child classes. Use interfaces if you only want to define method signatures without any code.

Summary

Abstract classes provide a base template that cannot be instantiated.

Abstract methods are declared without a body and must be implemented by subclasses.

They help organize code by sharing common behavior and enforcing specific implementations.