Abstract Classes and Methods
Two additional features of the C# polymorphism are abstract methods and abstract classes.
To define an abstract method the keyword: abstract must be placed in front of the method's definition and the abstract method can have no body as in the following code:
abstract public void theMethod();
In the case of classes, the keyword: abstract must appear before the word class in the definition of the class as in the following code:
abstract public class theClass
{
....
}
An abstract class must contain one or more abstract methods. Any class that contains an abstract method must be defined as an abstract class. An abstract class may not have any defined objects. In addition to the abstract methods, an abstract class may also have non abstract methods as well.
Abstract classes and methods must be defined public.
Any derived class of an abstract class must override the inherited abstract methods in order to permit the derived objects to be defined. If the abstract methods are not overridden, then the derived class would be considered abstract and any attempts to define objects for such a derived class would fail. As discussed above, this is done by placing the word override in front of the method's definition. For example see theAbstract1.cs.
While a class derived from an abstract class must implement each abstract method of the abstract class by overriding it, this is not true if a class is in turn derived from this derived class. For example see theAbstract2.cs.
One of the purposes of abstract classes is to provide a base from which other classes may be derived that in turn override the abstract methods. A second purpose is to permit the references of the abstract class to access the overridden methods as if they were references of the derived classes. For example see theAbstract3.cs.
// program_id theAbstract1.cs
// written by don voils
// date_written 10/3/2006
// Descriptioin The program shows how an abstract
// class and method can be defined as
// well as how to derive a class from the
// abstract class. If the keyword: override
// is removed from the definition of the
// method show() in the derived class,
// the program will not compile.
//
using System;
class theProgram
{
abstract public class theBase
{
private short theBaseMember;
public theBase(short theShort)
{
theBaseMember = theShort;
}
abstract public short show();
}
public class theDerived : theBase
{
private short theDerivedMember;
public theDerived(short firstShort, short secondShort)
: base(firstShort)
{
theDerivedMember = secondShort;
}
override public short show()
{
return theDerivedMember;
}
}
static void Main()
{
theDerived theDerivedReference = new theDerived(1, 2);
Console.WriteLine("The value of theBaseMember is {0}.\n\n",
theDerivedReference.show());
Console.ReadKey();
}
}
// program_id theAbstract2.cs
// written by don voils
// date_written 10/3/2006
// Descriptioin This program shows how an abstract
// class and method can be defined as
// well as how to derive a class from a class
// that is derived from an abstract class.
// In this case, this second derived class
// does not have to override the base class'
// abstract method.
//
using System;
class theProgram
{
abstract public class theBase
{
private short theBaseMember;
public theBase(short theShort)
{
theBaseMember = theShort;
}
abstract public short show();
}
public class theDerived : theBase
{
private short theDerivedMember;
public theDerived(short firstShort, short secondShort): base(firstShort)
{
theDerivedMember = secondShort;
}
override public short show()
{
return theDerivedMember;
}
}
public class nextDerived : theDerived
{
private short theNextDerivedMember;
public nextDerived(short firstShort, short secondShort, short thirdShort)
: base(firstShort, secondShort)
{
theNextDerivedMember = thirdShort;
}
public short showNextDerived()
{
return theNextDerivedMember;
}
}
static void Main()
{
nextDerived theReference = new nextDerived(1, 2, 3);
Console.WriteLine("The value of theNextDerivedMember is {0}.\n\n",
theReference.show());
Console.ReadKey();
}
}
// program_id theAbstract3.cs
// written by don voils
// date_written 10/3/2006
// Descriptioin This program shows how an abstract
// class and method can be defined as
// well as how to derive a class from a class
// that is derived from an abstract class.
// In this case, a second derived class
// is derived from the first derived class.
// In this program a reference to the abstract
// base class is used to access methods of
// each of the derived classes as if it was
// a reference to the respecitive classes.
// Without this base class being abstract,
// this reference would have been forced to
// access the base class' method which would
// have been impossible because it is in turn
// abstract.
//
using System;
class theProgram
{
abstract public class theBase
{
private short theBaseMember;
public theBase(short theShort)
{
theBaseMember = theShort;
}
abstract public short show();
}
public class theDerived : theBase
{
private short theDerivedMember;
public theDerived(short firstShort, short secondShort) : base(firstShort)
{
theDerivedMember = secondShort;
}
override public short show()
{
return theDerivedMember;
}
}
public class nextDerived : theDerived
{
private short theNextDerivedMember;
public nextDerived(short firstShort, short secondShort, short thirdShort)
: base(firstShort, secondShort)
{
theNextDerivedMember = thirdShort;
}
override public short show()
{
return theNextDerivedMember;
}
}
static void Main()
{
theDerived theDerivedReference = new theDerived(4, 5);
theBase theBaseReference = theDerivedReference;
Console.WriteLine("The value of theDerivedMember is {0}.\n",
theBaseReference.show());
nextDerived theNextDerivedReference = new nextDerived(1, 2, 3);
theBaseReference = theNextDerivedReference;
Console.WriteLine("The value of theNextDerivedMember is {0}.\n\n",
theBaseReference.show());
Console.ReadKey();
}
}
Wednesday, August 29, 2007
Subscribe to:
Post Comments (Atom)

No comments:
Post a Comment