Expand|Select|Wrap|Line Numbers
- 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.