#744 – The Purpose of Inheritance

Inheritance in C# (and .NET) is the idea of designing a class that reuses the implementation of an existing class.

The primary purpose of inheritance is to reuse code from an existing class.  Inheritance allows you to create a new class that starts off by including all data and implementation details of the base class.  You can then extend the derived class, to add data or behavior.

You can also use inheritance to modify the behavior of an existing class.  The code below shows a QuietDog class that does everything that a normal Dog does, but barks quietly.

    public class Dog
        public string Name { get; set; }

        public void Bark()

        public void Fetch()
            Console.WriteLine("Fetching !");

    public class QuietDog : Dog
        public new void Bark()
            Console.WriteLine(".. silence ..");

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #744 – The Purpose of Inheritance

  1. I think this is a bad example of inheritance. Using new brings issues when you treat the QuietDog as a Dog. Add virtual to the main Bark method in the Dog class and then replace new with override in the QuietDog class instead.



Leave a Reply to Imar Spaanjaars Cancel 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 )

Connecting to %s

%d bloggers like this: