#617 – The Simplest Way to Call a Method in a Base Class

In code for a derived class, you can call a method in the base class directly.

    public class Dog
    {
        public void Bark()
        {
            Console.WriteLine("Dog barking: Woof");
        }
    }

    public class Terrier : Dog
    {
        public void Growl()
        {
            Console.WriteLine("Terrier growling: Grrr!");
            Bark();   // Dog.Bark
        }
    }

Because the derived class (e.g. Terrier) inherits all of the methods of the base class, this code behaves as if Terrier had defined the Bark method itself.  The Bark method is called as an instance method on the instance for which the Growl method was called.

#335 – Accessing a Derived Class Using a Base Class Variable

You can use a variable whose type is a base class to reference instances of a derived class.

However, using the variable whose type is the base class, you can use it to access only members of the base class and not members of the derived class.

In the example below, we have two instances of the Terrier class, which derives from Dog.  One instance is referenced by a variable of type Terrier.  Using this variable, you have access to all members of the Terrier class.  On the other hand, the variable whose type is Dog can only reference members of the Dog class, even though the reference points to an instance of a Terrier.

            Terrier bubba = new Terrier("Bubba", 2, "Happy");
            bubba.Growl();    // Can call Terrier.Growl

            Dog jack = new Terrier("Jack", 17, "Surly");
            jack.Growl();     // ERROR: Can't call Growl method

#334 – A Base Class Variable Can Refer to Instances of Derived Classes

A variable whose type is a particular class can refer to instances of that class or it can refer to instances of any derived classes.

Assume that we have a Dog class (base class) and a Terrier class (derived class) that inherits from Dog.  A variable of type Dog can then refer to either a Dog or a Terrier.

Dog kirby = new Dog("Kirby", 15);

// Dog variable can point to Terrier
Dog jack = new Terrier("Jack", 17, "Surly");

// Terrier variable can also point to Terrier
Terrier bubba = new Terrier("Bubba", 2, "Happy");

Notice that even though the variable jack is of type Dog, it still points to an instance of a Terrier.

#332 – Every Class Inherits from Exactly One Class

Every class that you define in C# will inherit from exactly one class.

You can explicitly define a base class that the new class inherits from.

    public class Terrier : Dog

Or you can avoid specifying a base class when you define a new class.

    public class Terrier

If you don’t specify a base class, the new class will be automatically derived from System.Object.  You can see this by causing Intellisense to pop up for an instance of the class.  You see the class members of System.Object–e.g. Equals, GetHashCode, GetType, etc.

#330 – Derived Classes Do Not Inherit Constructors

A derived class inherits all of the members of a base class except for its constructors.

You must define a constructor in a derived class unless the base class has defined a default (parameterless) constructor.  If you don’t define a constructor in the derived class, the default constructor in the base class is called implicitly.

When you define a constructor in a derived class, you can call a constructor in the base class by using the base keyword.

Here’s an example:

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

        public Dog(string name, int age)
        {
            Name = name;
            Age = age;
        }
    }

    public class Terrier : Dog
    {
        public string Attitude { get; set; }

        // Call the Name/Age constructor in the base class
        public Terrier(string name, int age, string attitude)
            : base(name, age)
        {
            Attitude = attitude;
        }
    }

#329 – A Class Can Inherit Data and Behavior from Another Class

A class can inherit data and behavior from another class.  The new class (“derived class”) automatically includes all members of the class that it inherits from (“base class”) and can also define new members.

To inherit from another class, add a ‘:’ (colon) and the name of the base class in the definition of the derived class.

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

        public void Bark()
        {
            Console.WriteLine("{0} says woof", Name);
        }
    }

    public class Terrier : Dog
    {
        public string Attitude { get; set; }

        public void Growl()
        {
            Console.WriteLine("{0} says Grrrr", Name);
        }
    }

We can now create instances of both the Dog and the Terrier classes:

            Dog lassie = new Dog();
            lassie.Name = "Lassie";
            lassie.Age = 71;
            lassie.Bark();

            Terrier jack = new Terrier();
            jack.Name = "Jack";
            jack.Age = 17;
            jack.Attitude = "Aloof";
            jack.Bark();
            jack.Growl();