#615 – You Can’t Remove a Base Class Member

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.

Advertisements

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: