Override a parent class method in C#

[override]

One of the coolest features of object-oriented programming is virtual methods. If a parent class declares a method as virtual, a child class can provide a new implementation for the method. When the program calls the method for a child object, the child class’s version of the method is used.

The remarkable thing is that the child class’s version is used even if the variable referring to the child class has the type of the parent class. This makes more sense if you see an example. Consider the following Person class.

public class Person
{
    public string FirstName, LastName, Street, City, State, Zip;

    // Initializing constructor.
    public Person(string firstName, string lastName,
        string street, string city, string state,
        string zip)
    {
        FirstName = firstName;
        LastName = lastName;
        Street = street;
        City = city;
        State = state;
        Zip = zip;
    }

    // Display the person's address.
    public virtual void ShowAddress()
    {
        string txt = FirstName + " " + LastName + '\n' +
            Street + '\n' +
            City + "  " + State + "  " + Zip;
        MessageBox.Show(txt, "Person Address",
            MessageBoxButtons.OK,
            MessageBoxIcon.Information);
    }
}

The class defines several public fields and an initializing constructor. It then defines a ShowAddress method that displays the person’s name and address. (Notice the virtual keyword.)

Now consider the following Employee class.

public class Employee : Person
{
    public string MailStop;

    // Initializing constructor.
    public Employee(string firstName, string lastName,
        string street, string city, string state,
        string zip, string mailStop)
        : base(firstName, lastName, street, city, state, zip)
    {
        MailStop = mailStop;
    }

    // Display the employee's address.
    public override void ShowAddress()
    {
        string txt = FirstName + " " + LastName + '\n' +
            Street + '\n' +
            City + "  " + State + "  " + Zip + '\n' +
            MailStop;
        MessageBox.Show(txt, "Employee Address",
            MessageBoxButtons.OK,
            MessageBoxIcon.Information);
    }
}

This class adds a MailStop field to the address information. It defines an initializing constructor that uses the parent class’s constructor.

The class then overrides the parent’s ShowAddress method. (Notice the overrides keyword.) This version displays the address much as the parent class does but adds the MailStop at the end.

Here’s where the fun begins. The main program has three buttons that create Person and Employee objects saved in various kinds of variables. It then invokes the objects’ ShowAddress methods.

The first button uses the following code to create a Person object and store it in a Person variable.

// Create an Person variable that refers to
// an Person and call its ShowAddress method.
private void btnPerson_Click(object sender, EventArgs e)
{
    Person person = new Person("Rod", "Stephens",
        "1337 Leet St", "Hacker", "HI", "01234");
    person.ShowAddress();
}

This code displays an address with name, street, city, state, and ZIP. No big surprise here.

The second button uses the following code to create an Employee object and store it in an Employee variable.

// Create an Employee variable that refers to
// an Employee and call its ShowAddress method.
private void btnEmployee_Click(object sender, EventArgs e)
{
    Employee employee = new Employee("Rod", "Stephens",
        "1337 Leet St", "Hacker", "HI", "01234", "MS-17");
    employee.ShowAddress();
}

Again no surprises. This code displays the Employee version of the address with the MailStop added.

The third button uses the following code to create an Employee button. Because an Employee is a kind of Person, it can save the Employee in a variable of type Person.

// Create a Person variable that refers to
// an Employee and call its ShowAddress method.
private void btnEmployeeAsPerson_Click(object sender, EventArgs e)
{
    Person person = new Employee("Rod", "Stephens",
        "1337 Leet St", "Hacker", "HI", "01234", "MS-17");
    person.ShowAddress();
}

The ability of a program to treat an object as if it were of a parent type is called "polymorphism" and is one of the big features of object-oriented programming.

This code calls a Person object's ShowAddress method. That code doesn't really know that the object is an Employee because it is stored in a Person variable, but the object knows. Because the method is virtual and the object knows it is an Employee, it uses the Employee class's version of the method and displays an address that includes the MailStop.


Download Example   Follow me on Twitter   RSS feed   Donate




This entry was posted in classes, OOP and tagged , , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *