What is an abstract class?
An abstract class in C# provides a blueprint of what its derived classes must implement before they can use it. An abstract class contains abstract members including abstract methods, abstract properties, abstract indexers, and abstract events. All abstract members of a class must be implemented by its derived classes.
Here are the key characteristics of abstract classes,
- An abstract class cannot be instantiated.
- A class may inherit one abstract class only.
- An abstract class can have a constructor. In general, a class constructor is used to initialize fields. Along the same lines, an abstract class constructor is used to initialize fields of the abstract class.
- An abstract class may contain abstract methods, properties, and events.
- An abstract class can’t be modified with the sealed class modifier.
- A derived from an abstract class must implement all inherited abstract methods and accessors
How to declare abstract methods in C#
An abstract method is declared by using the abstract modifier in a method declaration to indicate that the method is an abstract method and does not contain implementation. An abstract method is implicitly a virtual method.
Abstract methods have no implementation, so the method definition is followed by a semicolon instead of a normal method block. Derived classes of the abstract class must implement all abstract methods. When an abstract class inherits a virtual method from a base class, the abstract class can override the virtual method with an abstract method.
- public abstract void AMethod();
The implementation is provided by a method override, which is a member of a non-abstract class.
How to declare abstract properties in C#
An abstract property is declared by using the abstract modifier in a property declaration to indicate that the property is an abstract method and does not contain implementation.
How to declare an abstract class in C#?
The abstract keyword is used to create an abstract class. The class definition starts with the abstract keyword.
The following code example declares an abstract base class, Shape.
- abstract public class Shape
- {
- }
The following code example declares an abstract base class, Shape, with an abstract method and an abstract property.
- // Abstract class
- abstract public class Shape
- {
- // Abstract properties
- public abstract int Height { set; }
- // Abstract method
- public abstract int Area();
- }
How to implement an abstract class in C#?
The following code implements the Shape abstract class by inheriting two classes, Rectangle and Square, from the class. Both derived classes must implement all abstract members. In our case, both classes implement Height property and Area method.
- // Rectangle class
- public class Rectangle : Shape
- {
- private int width;
- private int height;
- public Rectangle(int height)
- {
- this.height = height;
- }
- public override int Height { set => height = value; }
- public int Width {set => width = value; }
- public override int Area()
- {
- return this.height * this.width;
- }
- }
- // Square class
- public class Square : Shape
- {
- private int width = 0;
- private int height;
- public Square(int w)
- {
- this.width = w;
- }
- public override int Height { set => height = value; }
- public override int Area()
- {
- return this.width * this.width;
- }
- }
Complete Code Example
The following code example is the complete console application written in .NET Core.
- using System;
- namespace Abstractsample
- {
- class Program
- {
- static void Main(string[] args)
- {
- Square sq = new Square(10);
- Console.WriteLine($"Square area: {sq.Area()}");
- Rectangle rect = new Rectangle(50);
- rect.Width = 20;
- Console.WriteLine($"Rectangle area: {rect.Area()}");
- Console.ReadKey();
- }
- }
- // Rectangle class
- public class Rectangle : Shape
- {
- private int width;
- private int height;
- public Rectangle(int height)
- {
- this.height = height;
- }
- public override int Height { set => height = value; }
- public int Width {set => width = value; }
- public override int Area()
- {
- return this.height * this.width;
- }
- }
- // Square class
- public class Square : Shape
- {
- private int width = 0;
- private int height;
- public Square(int w)
- {
- this.width = w;
- }
- public override int Height { set => height = value; }
- public override int Area()
- {
- return this.width * this.width;
- }
- }
- // Abstract class
- abstract public class Shape
- {
- // Abstract properties
- public abstract int Height { set; }
- // Abstract method
- public abstract int Area();
- }
- }
No comments:
Post a Comment