Abstract Example
abstract class BaseClass // Abstract class
{
protected int _x = 100;
protected int _y = 150;
public abstract void AbstractMethod(); // Abstract method
public abstract int X { get; }
public abstract int Y { get; }
}
class Program : BaseClass
{
///
/// Abstract Class
///
/// Use the abstract modifier in a method or property declaration to indicate that the method
/// or property does not contain implementation.
/// Use the abstract modifier in a class declaration to indicate that a class is intended only
/// to be a base class of other classes.
/// The abstract modifier can be used with classes, methods, properties, indexers, and events.
///
/// -An abstract class cannot be instantiated.
/// -An abstract class class cannot be inherited.
/// -An abstract class may contain abstract methods and accessors.
/// -A non-abstract class derived from an abstract class must include actual implementations
/// of all inherited abstract methods and accessors.
///
/// Abstract methods
///
/// -An abstract method is implicitly a virtual method.
/// -Abstract method declarations are only permitted in abstract classes.
/// -It is an error to use the static or virtual modifiers in an abstract method declaration.
///
/// Abstract properties
///
/// -Abstract properties behave like abstract methods, except for the differences in declaration and invocation syntax.
/// -It is an error to use the abstract modifier on a static property.
/// -An abstract inherited property can be overridden in a derived class by including a property declaration that uses
/// the override modifier.
///
///
/// An abstract class must provide implementation for all interface members.
///
/*interface I
{
void M();
}
abstract class C: I
{
public abstract void M();
} */
public override void AbstractMethod()
{
_x++;
_y++;
}
public override int X // overriding property
{
get
{
return _x + 10;
}
}
public override int Y // overriding property
{
get
{
return _y + 10;
}
}
static void Main(string[] args)
{
Program o = new Program();
o.AbstractMethod();
Console.WriteLine("x = {0}, y = {1}", o.X, o.Y);
Console.ReadKey();
/*Output
x = 111, y = 161*/
}
}
Comments
Post a Comment