Wednesday, August 29, 2007

Base n Derived Class references : C#

References to a Base Class Manipulate Objects of the Derived Class
Polymorphism in C# is slightly different from that in C++. As you may recall, in C++ part of the polymorphism feature used pointers and part of it used references. Since C# does not use pointers but does use references, only the reference part is used.

For example, references to a base class may be used to reference a derived class' object. In this way, an object of a derived class may be treated as an object of the base class.

For example suppose a program has two classes: theBase and theDerived where theDerived is derived from theBase. Suppose the following definition of a reference of the derived class:



theDerived theDerivedReference = new theDerived();




With the following definition, the reference: theBaseReference references the derived object referenced by theDerivedReference.



theBase theBaseReference = theDerivedReference;




This last statement would then permit theBaseReference to manipulate the theBase class' contents of the object to which theDerivedRefence refers to.

For excample see theReference1.cs. One of the problems with this relationship is similar to the problem with pointers in C++. That is the reference: theBaseReference may only refer to the base class members of the derived class object. It can not refer to the derived class members of the object. Notice in this program where theBaseReference is trying to access the method: show_theDerived() if the comment symbols are removed, the program will not compile.

The reverse relationship is not possible. Meaning, that a reference to a derived class may not be used with an object of the base class. If a program does, then there will be a compiler error. For example if the following code replaced the code in the previous example the program would not compile:t



theBase theBaseReference = new theBase(2);
theDerived theDerivedReference;

theDerivedReference = theBaseReference;




See theReference3.cs.

In addition if anotherDerived is a class that is itself derived from theDerived class, then theBaseReference may also be used to access theBase class' part of an object of anotherDerived class' object. For example see theReference2.cs.


// program_id theReference1.cs
// written by don voils
// date_written 10/3/2006
// Descriptioin The program shows how references to a base
// class can be used to access the base class
// part of an object of the derived class.
//

using System;

class theProgram
{
public class theBase
{
private short theBaseMember;
public theBase(short theShort)
{
theBaseMember = theShort;
}
public short show_theBase()
{
return theBaseMember;
}
}

public class theDerived : theBase
{
private short theDerivedMember;
public theDerived(short firstShort, short secondShort)
: base(firstShort)
{
theDerivedMember = secondShort;
}
public short show_theDerived()
{
return theDerivedMember;
}
}

static void Main()
{

theBase theBaseReference;
theDerived theDerivedReference = new theDerived(1, 2);

theBaseReference = theDerivedReference;

Console.WriteLine("The value of theBaseMember is {0}.\n",
theBaseReference.show_theBase());

//
// The following line will not compile. Why?
//
// Console.WriteLine("The value of theDerivedMember is {0}.\n",
// theBaseReference.show_theDerived());

Console.WriteLine("\n\n");
Console.ReadKey();
}


}



// program_id theReference2.cs
// written by don voils
// date_written 10/3/2006
// Descriptioin The program shows how references to a base
// class can be used to access the base class
// part of an object of a class that is derived from a
// class that is derived from the base.
//

using System;

class theProgram
{
public class theBase
{
private short theBaseMember;
public theBase(short theShort)
{
theBaseMember = theShort;
}
public short show_theBase()
{
return theBaseMember;
}
}

public class theDerived : theBase
{
private short theDerivedMember;
public theDerived(short firstShort, short secondShort)
: base(firstShort)
{
theDerivedMember = secondShort;
}
public short show_theDerived()
{
return theDerivedMember;
}

}

public class anotherDerived : theDerived
{
private short anotherDerivedMember;
public anotherDerived(short firstShort, short secondShort, short thirdShort)
: base(firstShort, secondShort)
{
anotherDerivedMember = thirdShort;
}
public short show_anotherDerived()
{
return anotherDerivedMember;
}
}

static void Main()
{
theBase theBaseReference;
theDerived theDerivedReference = new theDerived(1, 2);

theBaseReference = theDerivedReference;

Console.WriteLine("The value of theBaseMember is {0}.\n",
theBaseReference.show_theBase());

anotherDerived anotherDerivedReference = new anotherDerived(5, 6, 7);

theBaseReference = anotherDerivedReference;

Console.WriteLine("The value of theBaseMember is {0}.\n\n",
theBaseReference.show_theBase());

Console.ReadKey();
}


}



// program_id theReference3.cs
// written by don voils
// date_written 10/3/2006
// Descriptioin The program shows that a reference to
// a derived class may not be used to reference
// a base class object.
//

using System;

class theProgram
{
public class theBase
{
private short theBaseMember;
public theBase(short theShort)
{
theBaseMember = theShort;
}
public short show_theBase()
{
return theBaseMember;
}
}

public class theDerived : theBase
{
private short theDerivedMember;
public theDerived(short firstShort, short secondShort)
: base(firstShort)
{
theDerivedMember = secondShort;
}
public short show_theDerived()
{
return theDerivedMember;
}
}

static void Main()
{
theBase theBaseReference = new theBase(2);
theDerived theDerivedReference;

theDerivedReference = theBaseReference;

Console.WriteLine("\n\n");
Console.ReadKey();
}


}

No comments: