You can have constructor in Abstract Class but cannot instantiate the class.
using System;
using System.Collections.Generic;
using System.Text;
namespace TestProject
{
public class Mains
{
public static void Main()
{
//This will give error:
//Cannot create an instance of the abstract class or interface 'TestProject.Abstract'
//Abstract abs = new Abstract(4);
//abs.Print();
//This will work fine
Abstract abs = new First(4,5);
abs.Print();
}
}
abstract class Abstract
{
protected int i;
//Abstract()
//{
//}
public Abstract(int i)
{
this.i = i;
}
public virtual void Print()
{
Console.WriteLine(i);
Console.Read();
}
}
class First : Abstract
{
int k;
public First(int i, int j)
: base(i)
{
this.k = j;
}
public override void Print()
{
Console.WriteLine("i= " + base.i + " k= " + k);
Console.Read();
}
}
}
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment