Tuesday, November 18, 2008

Multiple Abstract Class

You can derive an abstract class from another abstract class. In that case, in the child class it is optional to make the implementation of the abstract methods of the parent class.

Example:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
public abstract class Class1
{
public void Add(int n1, int n2)
{
int n3;
n3= n1 + n2;
Console.WriteLine("N3:" + n3);
Console.ReadLine();
}
public abstract void Multiply(int x, int y);
}
public abstract class Program1 : Class1
{
string c;
public void Concatenate(string a, string b)
{
c = a + b;
Console.WriteLine(" C:" + c);
Console.ReadLine();
}
}
class Program : Program1
{

public static void Main(string[] args)
{
Program asd = new Program();
asd.Add(10, 20);
asd.Concatenate("Hem", "nath");
asd.Multiply(20, 100);
}
public override void Multiply(int e, int f)
{
int z;
z = e * f;
Console.WriteLine("Z:" + z);
Console.ReadLine();
}
}
}

In the above example, absClass1 contains two methods Add and abstract method Multiply. The class Program is derived from Program1 and the Multiply is implemented there.

No comments: