If you compile this you get a warning saying that "
'BIOS.classHeader' hides inherited member 'Hardware.classHeader'. Use the new keyword if hiding was intended. If you use the new it will function correctly but not use a single method. " which gives you a clue.
If you remove the following line from the subclass
BIOS
your code functions as you'd expect:
public string classHeader;
In your original code (with the above declaration in place) the
classHeaderPropety
in
BIOS
hides the underlying
classHeader
in the
Hardware
class. As the
DisplayInfo
method in your code is defined in the
Hardware
class it uses the
classHeader
from the base
Hardware
not the one hiding it in
BIOS
which the method is "unaware" of, giving you your results.
To help explain (not for real use!), try changing your
original BIOS
class so it has a secondary method that writes out the display directly, nit via the superclass, e.g. to this:
class BIOS : Hardware
{
public string classHeader;
public BIOS()
{
classHeader = "This is the BIOS class";
}
public void DisplayInfoBIOS()
{
Console.WriteLine(classHeader);
}
}
and adding this line to your main function before the readkey:
MyBIOS.DisplayInfoBIOS();
As the new
DisplayInfoBIOS
is defined in the
BIOS
class, it uses the
BIOS
's version of
classHeader
, and you get the BIOS text.
As as final exercise to convince yourself, try changing the new
DisplayInfoBIOS
from:
Console.WriteLine(classHeader);
to
Console.WriteLine(base.classHeader);
and try and predict the result.
Obviously the thing to do is remove the line I mentioned above so both super and sub classes access the same member rather than hacking an new method in :)