#615 – You Can’t Remove a Base Class Member
June 28, 2012 Leave a comment
A subclass class inherits public members from its base class. Below, the Terrier class inherits the Name property and the Bark method from the Dog class.
public class Dog { public string Name { get; set; } public void Bark() { Console.WriteLine("Woof"); } } public class Terrier : Dog { public string Temperament { get; set; } }
Terrier jack = new Terrier(); jack.Bark(); // Woof
The Terrier class can provide a new implementation of the Bark method:
public class Terrier : Dog { public string Temperament { get; set; } public new void Bark() { Console.WriteLine("Grrr, growf!"); } }
But you can’t completely remove the Bark method from the Terrier class. If you try making the Bark method in Terrier private, when you invoke the Bark method on a Terrier object, the method in the parent Dog class will be called instead.