C# Tutorial
C# String
C# Array
C# Flow Control
C# Class and Object
C# Inheritance
C# Interface
C# Collection
C# Generic
C# File I/O
C# Delegate and Event
C# Exception
C# Process and Thread
C# ADO.NET Database Operations
The abstract
keyword in C# is used to declare abstract classes or abstract methods. An abstract class cannot be instantiated, and it is intended to be inherited by other classes. An abstract method is a method declaration without an implementation, which means the derived class must provide the implementation.
Here's a tutorial on using the abstract
keyword to declare abstract classes and abstract methods in C#:
To declare an abstract class, use the abstract
keyword before the class
keyword:
public abstract class Animal { // class members }
You cannot create instances of an abstract class:
Animal animal = new Animal(); // This will cause a compile-time error
To declare an abstract method, use the abstract
keyword before the method signature and end the declaration with a semicolon ;
instead of a method body:
public abstract class Animal { public abstract void Speak(); }
An abstract method doesn't have a method body and cannot have an access modifier other than public
, protected
, or internal
.
To inherit from an abstract class, use the :
operator followed by the name of the abstract class:
public class Dog : Animal { // class members }
When you inherit from an abstract class, you must provide an implementation for all its abstract methods in the derived class. To do this, use the override
keyword before the method signature and provide a method body:
public class Dog : Animal { public override void Speak() { Console.WriteLine("The dog barks."); } }
Here's an example that demonstrates using an abstract class and abstract methods:
using System; public abstract class Animal { public abstract void Speak(); } public class Dog : Animal { public override void Speak() { Console.WriteLine("The dog barks."); } } public class Cat : Animal { public override void Speak() { Console.WriteLine("The cat meows."); } } class Program { static void Main() { Animal dog = new Dog(); dog.Speak(); // Output: The dog barks. Animal cat = new Cat(); cat.Speak(); // Output: The cat meows. } }
In this example, we've declared an abstract class Animal
with an abstract method Speak()
. We've then created two derived classes, Dog
and Cat
, which inherit from the Animal
class and provide their own implementations of the Speak()
method. In the Main
method, we've created instances of the Dog
and Cat
classes and called their Speak()
methods.
C# Abstract Class and Abstract Method Example:
// Abstract class declaration public abstract class Shape { // Abstract method declaration public abstract double CalculateArea(); } // Concrete class inheriting from the abstract class public class Circle : Shape { // Implementation of the abstract method public override double CalculateArea() { // Calculation logic specific to Circle return Math.PI * Radius * Radius; } // Additional properties and methods for Circle public double Radius { get; set; } }
How to Declare Abstract Class in C# with Abstract Keyword:
// Declaration of an abstract class with an abstract method public abstract class Animal { // Abstract method public abstract void MakeSound(); // Concrete method public void Sleep() { Console.WriteLine("Zzzz"); } }
Abstract Class Inheritance in C#:
// Abstract class declaration public abstract class Vehicle { // Abstract method public abstract void Start(); // Concrete method public void Stop() { Console.WriteLine("Vehicle stopped"); } } // Concrete class inheriting from the abstract class public class Car : Vehicle { // Implementation of the abstract method public override void Start() { Console.WriteLine("Car started"); } // Additional properties and methods for Car public void Accelerate() { Console.WriteLine("Car accelerating"); } }