Intro to OOP in C#: Inheritance - Page 2
       by kirupa  |  11 February 2007

In the previous page you learned what inheritance is and why it is important. Let's now look at some code to see it all work. Below, I have provided the code for the Character class as well as a Program class that contains our Main method:

class Character
{
public void Walk()
{
Console.WriteLine("Character walking!");
}
 
public void Talk()
{
Console.WriteLine("Character is talking about something");
}
 
public void Say(string thingToSay)
{
Console.WriteLine("Character says: {0}", thingToSay);
}
 
}
 
class Program
{
static void Main(string[] args)
{
Character foo = new Character();
foo.Say("Hello World!");
}
}

The above code is very straightforward, and any interesting things you see in the above code is largely covered by my earlier Classes tutorial. If you run the program, you will see Character says: Hello World! displayed on the screen.

If you look at the AutoComplete for the foo object, you should see the following:

[ the methods available to our Character object foo ]

Notice that you can access not only the default Object methods Equals, GetHashCode, GetType, and ToString, but also the Say, Talk, and Walk methods you wrote yourself.

Extending a Class
At this point, we just wrote our Character class. What we really want to do is use our Character class to build our Alien, Bandit, Cowboy, and Pirate characters. Before we continue, let's clarify what some weird words mean.

You'll see the word extending used frequently from here on out. Basing one class on another is called extending the class. For example, if we were to create an Alien class that is based on the Character class, we can say that Alien extends Character. Finally, the word base refers to the class you are extending from. In our examples, the base class is Character since we are extending it to create our more specialized characters.

Let's get back to the code. Since I am a big X-Files fan, let's go ahead and create an Alien class in C#:

class Character
{
public void Walk()
{
Console.WriteLine("Character walking!");
}
 
public void Talk()
{
Console.WriteLine("Character is talking about something");
}
 
public void Say(string thingToSay)
{
Console.WriteLine("Character says: {0}", thingToSay);
}
}
 
class Alien : Character
{
public void Teleport(string currentLocation, string newLocation)
{
Console.WriteLine("The alien teleported from {0} to {1}", currentLocation, newLocation);
}
 
public void Hide()
{
Console.WriteLine("The alien is hiding.");
}
}
 
class Program
{
static void Main(string[] args)
{
Character foo = new Character();
foo.Say("Hello World!");
}
}

The Alien class looks like any other class definition, but there is one major difference. Notice the : Character after the Alien text in the class declaration. The : (colon) is shorthand for extends, as in, this class will be extended by Character.

Our Alien class itself only contains the Teleport and Hide methods, but because we extended the Character class, you also have access to the methods defined in the Character class:

[ all of the methods you now have access to ]

This means that your Alien can not only teleport and hide, but it can also say, talk, and walk like a normal Character. Best of all, you only had to write your own teleport and hide functionality. There will be situations where you may not want to have access to the functionality from your base class, and I'll cover how to deal with that in the next page.

Onwards to the next page!

1 | 2 | 3




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.