Showing posts with label abstract. Show all posts
Showing posts with label abstract. Show all posts

Tuesday, 11 March 2014

Abstract Class in C#

Abstract Class in C#

An abstract method has no implementation. Its implementation logic is provided instead by classes that derive from it. We use an abstract class to create a base template for derived classes.
Key Points:
Abstract classes have the following features:
  • An abstract class cannot be instantiated.
  • An abstract class may contain abstract methods and accessors.
  • It is not possible to modify an abstract class with the sealed (C# Reference) modifier because the two modifers have opposite meanings. The sealedmodifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
  • A non-abstract class derived from an abstract class must include actual implementations of all inherited abstract methods and accessors.

public abstract class Maths
{
    public abstract int sum(int x, int y)
    public int multiply(int x, int y) 
    {
        return (x * y);
    }
}
public class Test : Maths
{
    public static void Main()
    {
        Test T = new Test();
        Console.WriteLine("Enter value of X:");
        int x = int.Parse(Console.ReadLine());
        Console.WriteLine("Enter value of Y:");
        int y = int.Parse(Console.ReadLine());
        int Sum = T.sum(x,y);
        Console.WriteLine("SUM of " + x + " and " + y + " IS:" + Sum);
        int Multiply = T.multiply(a, b);
        Console.WriteLine("MULTIPLICATION of " + x + " and " + y + " IS:" + Multiply);
        Console.Read();
    }
    public override int sum(int x, int y) 
    {
        return (x + y);
    }
}