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.
Derived classes. When you create a derived class like Example1 or Example2, you must provide an override method for all abstract methods in the abstract class. The A() method in both derived classes satisfies this requirement.
Override
Int field. An abstract class can have an instance field in it. The derived classes can access this field through the base syntax. This is a key difference between abstract classes and interfaces.
Int
Cannot instantiate abstract class. The important part of an abstract class is that you can never use it separately from a derived class. Therefore in Main you cannot use the new Test() constructor.
However:You can use the Test type directly once you have assigned it to a derived type such as Example1 or Example2.
Code:
using System;
abstract class Test
{
public int _a;
public abstract void A();
}
class Example1 : Test
{
public override void A()
{
Console.WriteLine("Example1.A");
base._a++;
}
}
class Example2 : Test
{
public override void A()
{
Console.WriteLine("Example2.A");
base._a--;
}
}
class Program
{
static void Main()
{
// Reference Example1 through Test type.
Test test1 = new Example1();
test1.A();
// Reference Example2 through Test type.
Test test2 = new Example2();
test2.A();
}
}
Override
Int field. An abstract class can have an instance field in it. The derived classes can access this field through the base syntax. This is a key difference between abstract classes and interfaces.
Int
Cannot instantiate abstract class. The important part of an abstract class is that you can never use it separately from a derived class. Therefore in Main you cannot use the new Test() constructor.
However:You can use the Test type directly once you have assigned it to a derived type such as Example1 or Example2.