
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
What are abstract classes in C#?
Abstract classes contain abstract methods, which are implemented by the derived class. The derived classes have more specialized functionality.
The following is an example showing the usage of abstract classes in C#.
Example
using System; namespace Demo { abstract class Shape { public abstract int area(); } class Rectangle: Shape { private int length; private int width; public Rectangle( int a = 0, int b = 0) { length = a; width = b; Console.WriteLine("Length of Rectangle: "+length); Console.WriteLine("Width of Rectangle: "+width); } public override int area () { return (width * length); } } class RectangleTester { static void Main(string[] args) { Rectangle r = new Rectangle(14, 8); double a = r.area(); Console.WriteLine("Area: {0}",a); Console.ReadKey(); } } }
Output
Length of Rectangle: 14 Width of Rectangle: 8 Area: 112
Our abstract class above is −
abstract class Shape { public abstract int area(); }
The following are the rules about abstract classes.
- You cannot create an instance of an abstract class
- You cannot declare an abstract method outside an abstract class
- When a class is declared sealed, it cannot be inherited, abstract classes cannot be declared sealed.
- Related Articles
- What are abstract classes in Java?
- Abstract Classes in Java
- Abstract Classes in C#
- Abstract Classes in Dart Programming
- Abstract Method and Classes in Java
- Abstract Base Classes in Python (abc)
- How to create abstract classes in TypeScript?
- Python Abstract Base Classes for Containers
- What is the difference between interfaces and abstract classes in Java?
- Pure Virtual Functions and Abstract Classes in C++
- Difference between Traits and Abstract Classes in Scala.
- Abstract vs Sealed Classes vs Class Members in C#
- what are abstract methods in Java?
- What are abstract properties in C#?
- What are Java classes?

Advertisements