Abstract Class in C#
In C#, abstract class is a class which is declared abstract. It can have abstract and non-abstract methods. It cannot be instantiated. Its implementation must be provided by derived classes. Here, derived class is forced to provide the implementation of all the abstract methods.
Abstract classes are the way to achieve abstraction in C#. Abstraction in C# is the process to hide the internal details and showing functionality only. Abstraction can be achieved by two ways:
1. Abstract class
2. Interface
Abstract class and interface both can have abstract methods which are necessary for abstraction. But we can’t use a non-abstract method in Interface.
A method which is declared abstract and has no body is called abstract method. It can be declared inside the abstract class only. Its implementation must be provided by derived classes.
Structure of Abstract:
abstract class MyBaseClass
{
// Abstract method declaration
public abstract void MyAbstractMethod();
// Non-abstract method
public void MyConcreteMethod()
{
Console.WriteLine("This is a concrete method in the abstract class.");
}
}
Scenario: Create a program to simulate different pets. Each pet can make its own sound and all pets can sleep. You will use an abstract class for common behaviours and specific classes for each pet.
using System;
abstract class Animal
{
public abstract void MakeSound();
public void Sleep()
{
Console.WriteLine(“The animal is sleeping.”);
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine(“The dog says: Woof Woof”);
}
}
class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine(“The cat says: Meow Meow”);
}
}
class Program
{
static void Main()
{
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.MakeSound();
myCat.MakeSound();
myDog.Sleep();
myCat.Sleep();
}
}
Explanation:
using System;
// Define an abstract class named Animal
abstract class Animal
{
// Abstract method that must be implemented by derived classes
public abstract void MakeSound();
// Concrete method that is common to all animals
public void Sleep()
{
Console.WriteLine(“The animal is sleeping.”);
}
}
// Dog class inherits from Animal
class Dog : Animal
{
// Override the MakeSound method to specify dog’s sound
public override void MakeSound()
{
Console.WriteLine(“The dog says: Woof Woof”);
}
}
// Cat class inherits from Animal
class Cat : Animal
{
// Override the MakeSound method to specify cat’s sound
public override void MakeSound()
{
Console.WriteLine(“The cat says: Meow Meow”);
}
}
// Main program class
class Program
{
static void Main()
{
// Create instances of Dog and Cat
Dog myDog = new Dog();
Cat myCat = new Cat();
// Call the MakeSound method for dog and cat
myDog.MakeSound(); // Outputs: The dog says: Woof Woof
myCat.MakeSound(); // Outputs: The cat says: Meow Meow
// Call the Sleep method for dog and cat
myDog.Sleep(); // Outputs: The animal is sleeping.
myCat.Sleep(); // Outputs: The animal is sleeping.
}
}
Task:
1. Basic Abstract Class and Method Implementation:
Create an abstract class Vehicle with an abstract method Start. Derive two classes Car and Bike from Vehicle. Implement the Start method in both derived classes to print “Car starting” and “Bike starting” respectively. In the Main method, create instances of Car and Bike and call the Start method on each.
2. Abstract Class with Properties:
Create an abstract class Person with an abstract property Name and an abstract method GetDetails. Derive two classes Student and Teacher from Person. Implement the Name property and GetDetails method in both derived classes. In the Main method, create instances of Student and Teacher, set their names, and call the GetDetails method to print their details.
3. Abstract Class with Constructor :
Create an abstract class Appliance with a protected constructor that takes a string parameter brand and an abstract method ShowBrand. Derive two classes WashingMachine and Refrigerator from Appliance. Implement the ShowBrand method in both derived classes to print the brand. In the Main method, create instances of WashingMachine and Refrigerator and call the ShowBrand method.
4. Abstract Class with Multiple Methods:
Create an abstract class Shape with two abstract methods: CalculateArea and CalculatePerimeter. Derive two classes Circle and Rectangle from Shape. Implement the CalculateArea and CalculatePerimeter methods in both derived classes. In the Main method, create instances of Circle and Rectangle, set their dimensions, and call the methods to print the area and perimeter of each shape.
5. Abstract Class and Inheritance Hierarchy:
Create an abstract class Animal with an abstract method Speak. Derive a class Mammal from Animal and override the Speak method. Further, derive two classes Dog and Cat from Mammal and provide their specific implementations of the Speak method. In the Main method, create instances of Dog and Cat and call their Speak methods.
6. Abstract Class with Virtual and Abstract Methods:
Create an abstract class Employee with a virtual method Work that prints “Working” and an abstract method GetSalary. Derive two classes FullTimeEmployee and PartTimeEmployee from Employee. Override the Work method in both derived classes to provide specific implementations and implement the GetSalary method. In the Main method, create instances of FullTimeEmployee and PartTimeEmployee and call their methods.