When using the "new" keyword, the base class uses its own property
implementation and the subclass, by default, uses its own property
implementation. That is, until you qualify the property access with the
"base" keyword (for example, base.Bold), the subclass would use its own
implementation. Just as you would expect - no tricks. So let's look at an
example.
There are two classes below, A and B. B derives, or inherits, from A. A is a
superclass (base class). B is a subclass (child class).
public class A : System.Object
{
private bool bold = false;
public bool Bold
{
get
{
return bold;
}
set
{
bold = value;
}
}
public void Display()
{
MessageBox.Show(Bold.ToString());
}
}
public class B : A
{
private bool bold = true;
public new bool Bold
{
get
{
return bold;
}
set
{
bold = value;
}
}
}
If you were to compile and run the code below, what would you expect to see?
A a = new A();
a.Display();
You would see "False". Since the call to the Display() method is against an
object of type "A" and Bold holds a value of "false". Now what if we changed
the code as shown below. What would be displayed?
B b = new B();
b.Display();
"False". Since "B" simply inherits the Display() method from "A", and, since
Display() is defined in the base class, the base classes implementation of
the Bold property is used. Now what if we changed the Bold property to be
"virtual" in the base class and "override" in the derived class, as shown
below.
public class A : System.Object
{
private bool bold = false;
public virtual bool Bold
{
get
{
return bold;
}
set
{
bold = value;
}
}
public void Display()
{
MessageBox.Show(Bold.ToString());
}
}
public class B : A
{
private bool bold = true;
public override bool Bold
{
get
{
return bold;
}
set
{
bold = value;
}
}
}
Now, if you were to compile and run the code below, what would you expect to
see?
A a = new A();
a.Display();
You would still see "False". Since the call to the Display() method is still
against an object of type "A" and Bold holds a value of "false". Now what if
we changed the code as shown below. What would be displayed?
B b = new B();
b.Display();
This time you will see "True". Why? In this case, because of the base
property being "virtual" and the inheriting class property being "override",
which implementation is used when accessing the Bold property is up to the
actual type that is making the call. So since "B" is the type, the Bold
property implementation in "B" is used. To learn more about how
virtual/override is used in different situations look into "polymorphism".
So now lets, hopefully, answer your question.
Suppose their is a property called Bold that I can't override. I
declare it as "new" and give it a default value. When the base
class of that component uses the Bold property to do the painting,
will it see it's own base class version or will it see my new
implementation of it? Isn't my new version "hidden" from the
base class?
The base class would see its own implementation since the virtual/override
mechanism is not in place. However, you can wrap the base property as shown
in the code below.
public class B : A
{
private bool bold = true;
public new bool Bold
{
get
{
return base.Bold;
}
set
{
base.Bold = value;
}
}
public B()
{
base.Bold = bold;
}
}
Hope that helps.