C# Abstract Classes

    1 Votes

The abstract classes are defined by the keyword abstract and are used to define a base class. They are the one of the essential feature provided by C#. They are used when we don’t want to create objects of the base class. The abstract class can contain either the abstract methods or non abstract methods. Rules to be applied:

  • Abstract Class cannot be sealed class.
  • The declaration of abstract methods is allowed in abstract classes only.
  • The abstract method cannot be private.
  • The abstract method cannot have the modifier virtual because method is implicitly virtual.
  • The abstract method cannot be static.

 

using System;
namespace abstractSample
{
	//Creating an Abstract Class
	abstract class absClass
	{
	    //A Non abstract method
	    public int add(int a, int b)
	    {
		return a + b;
	    }

	    //An abstract method, to be
	    //overridden in derived class
	    public abstract int mul(int a, int b);
	}

	//A Child Class of absClass
	class absDerived:absClass
	{
	    static void Main(string[] args)
	    {
	       absDerived cal = new absDerived();
	       int added = cal.add(10,20);
	       int multiplied = cal.mul(10,2);
	       Console.WriteLine("Addition is: {0}, Multiplication is: {1}", added, multiplied);
	    }
	    public override int mul(int a, int b)
	    {
		return a * b;
	    }
	}
}
Output: Addition is: 30, Multiplication is: 20

 

Abstract Classes and Class Members

The abstract keyword allows creating classes and class members which are incomplete and should be implemented in a derived class. The classes can be declared as abstract by using keyword abstract before the class definition. For example:

public abstract class Square
{
    // Class members here.
}

The abstract class cannot be instantiated. Basically, the purpose of an abstract class is to provide a common definition of a base class where multiple derived classes can share. For example, a class Square define an abstract class where it is used as a parameter to many of its functions, and the programmers using that Square are required to provide their own implementation of the class by creating a derived class.

Abstract classes also define abstract methods and this can be accomplished by adding the keyword abstract before the return type of the method. For example:

public abstract class Square
{
    public abstract void Area(int s);
}

The abstract class can inherit a virtual method from a base class, the abstract class can override the virtual method with an abstract method. For example:

public class Shape
{
    public virtual void Area(int i)
    {
        // Original implementation.
    }
}

public abstract class Square : Shape
{
    public abstract override void Area(int i);
}

public class Rectangle : Square
{
    public override void Area(int i)
    {
        // New implementation.
    }
}

Sealed Classes and Class Members

The classes can be declared as sealed by using the keyword sealed before the class definition. For example:

public sealed class Square
{
    // Class members here.
}

The sealed class cannot be used as a base class and because of this reason; it cannot be an abstract class. Sealed classes prevent the derivation, because they can never be used as a base class.

A method, indexer, property, or event, on a derived class that override a virtual member of the base class can be declared as sealed member. This can be accomplished by using the sealed keyword before the override keyword in the class member declaration. For example:

public class A : B
{
    public sealed override void Work( ) { }
}

Let us have an example to understand it better:

using System;
namespace Application
{
class Program
{
    static void Main(string[] args)
    {
        SealedClass sc = new SealedClass();
        int total = sc.add(2, 5);
        Console.WriteLine("The total is: " + total.ToString());
    }
}
// Sealed class
sealed class SealedClass
{
    public int add(int x, int y)
    {
        return x + y;
    }
}
}

Output: The total is: 7

Planning to do an MBA?
A quick all-in-one MBA entry manual for MBA Aspirants. Book covers
  • Possible MBA Tests & Exam Preparation
  • Tips to choose right MBA Program
  • Essay, Resume & Letter of Recommendation
  • MBA Interview Preparation
  • MBA Financial Planning
Price - 6.99$
 

Popular Videos

How to speak to people

How to speak so that people want to listen.

Got a tip or Question?
Let us know

Related Articles

C# Overview & Evolution of C# Language
Introduction of .NET Framework
C# Datatypes - Datatype Conversion, Boxing & Unboxing
C# Metadata, Metadata Tables and Benefits
C# - Common Language Intermediate (CIL)
C# - Class and Objects
C# Assembly - How to create and share assembly
C# Garbage Collection
C# Indexers
C# Exception Handling - User-Defied Exceptions
C# Inheritance - Single, Hierarchical, Multilevel & Multiple Inheritance
C# Polymorphism - Static and Dynamic polymorphism
C# Structures
C# Delegates and Events
C# Collections
C# XML Read and Write
C# Windows Presentation Foundation (WPF)
Language Integrated Query (LINQ)