Abstract Class
- "Abstract classes are useful when you need a class for the purpose of inheritance and polymorphism"
- The purpose of an abstract class is to provide default functionality to its subclasses
- When a class contains at least one abstract method, then the class must be declared as an abstract class
- It is mandatory to override the abstract method in the derived class
- When a class is declared as an abstract class, then it is not possible to create an instance for that class
- But it can be used as a parameter in a method
using System;
public abstract class Calculation
{
public abstract int sum(int a, int b); // an abstract class has no body ie no implementaiton
public int multiply(int a, int b) // this is non-abstract method
{
return (a * b);
}
}
public class Test : Calculation
{
public static void Main()
{
Test T = new Test();
Console.WriteLine("Enter value of A:");
int a = int.Parse(Console.ReadLine());
Console.WriteLine("Enter value of B:");
int b = int.Parse(Console.ReadLine());
int Sum = T.sum(a, b);
Console.WriteLine("SUM of " + a + " and " + b + " IS:" + Sum);
int Multiply = T.multiply(a, b);
Console.WriteLine("MULTIPLICATION of " + a + " and " + b + " IS:" + Multiply);
Console.Read();
}
public override int sum(int a, int b) // while calling abstract method use override with same modifier
{
return (a + b);
}
}