#742 – A Simple Example of Inheritance

Here’s a simple example of inheritance in C#.

The Dog class has a Name property and a Bark method.

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

        public void Bark()
            Console.WriteLine(Name + " : Woof");

The Terrier class inherits from Dog and adds a HuntVermin method.

    public class Terrier : Dog
        public void HuntVermin()
            Console.WriteLine(Name + " off to find vermin");

An object of type Dog has access to the Name property and the Bark method.  An object of type Terrier also has access to Name and Bark and also to the HuntVermin method.

            Dog d = new Dog();
            d.Name = "Kirby";

            Terrier t = new Terrier();
            t.Name = "Jack";