The new
keyword hides the original property. Because your Book
class has a protected Price
property, and the TextBook
class has a public property, the compiler is recognizing them as 2 different properties, the one in the TextBook
class as a completely new and unrelated property. When accessing the Price
, you will still end up with the price from the base class (Book
)
Try using virtual
and override
instead, as well as keeping the access modifiers consistent (They should both be public)
class Book
{
...
public virtual double Price { get { return price; } set { price = value; } } //Property
protected double price; //Backing Field
...
}
class TextBook : Book
{
...
public override double Price
{
set
{
if (value <= MIN)
price = MIN;
if (value >= MAX)
price = MAX;
else
price = value;
}
}
...
}
See more on Override/Virtual vs New on another question. Specifically from it, this diagram in the answers: