Polymorphism in C# - The Root of All Classes: Object
(Page 4 of 6 )
All C# classes, of any type, ultimately derive from a single class: Object.Objectis the base class for all other classes.
A base class is the immediate “parent” of a derived class. A derived class can be the base to further derived classes, creating an inheritance tree or hierarchy. A root class is the topmost class in an inheritance hierarchy. In C#, the root class isObject. The nomenclature is a bit confusing until you imagine an upside-down tree, with the root on top and the derived classes below. Thus, the base class is considered to be “above” the derived class.
Object provides a number of methods that subclasses can override. These includeEquals(), which determines if two objects are the same, andToString(), which returns a string to represent the current object. Specifically,ToString()returns a string with the name of the class to which the object belongs. Table 11-1 summarizes the methods ofObject.
Table 11-1. The Object class
| Method | What it does |
| Equals() | Evaluates whether two objects are equivalent |
| GetHashCode() | Allows objects to provide their own hash function for use in collections (see Chapter 14) |
| GetType() | Provides access to the Type object |
| ToString() | Provides a string representation of the object |
| Finalize() | Cleans up nonmemory resources; implemented by a destructor (finalizer) |
In Example 11-4, theDog class overrides theToString()method inherited fromObject, to return the weight of theDog.
Example 11-4. Overriding ToString
using System;
public class Dog
{
private int weight;
// constructor
public Dog( int weight )
{
this.weight = weight;
}
// override Object.ToString
public override string ToString()
{
return weight.ToString();
}
}
public class Tester
{
static void Main()
{
int i = 5;
Console.WriteLine( "The value of i is: {0}", i.ToString() );
Dog milo = new Dog( 62 );
Console.WriteLine( "My dog Milo weighs {0} pounds", milo);
}
}
Output:
The value of i is: 5
My dog Milo weighs 62 pounds
Some classes (such asConsole) have methods that expect a string (such asWriteLine()). These methods will call theToString()method on your class if you’ve overridden the inheritedToString()method fromObject. This lets you pass aDog toConsole.WriteLine, and the correct information will display.
This example also takes advantage of the startling fact that intrinsic types (int,long, etc.) can also be treated as if they derive fromObject, and thus you can callToString()on anintvariable! CallingToString()on an intrinsic type returns a string representation of the variable’s value.
The documentation forObject.ToString()reveals its signature:
public virtual string ToString();
It is a public virtual method that returns a string and takes no parameters. All the built-in types, such asint, derive fromObjectand so can invokeObject’s methods.
TheConsole class’sWrite()andWriteLine()methods callToString()for you on objects that you pass in for display. Thus, by overridingToString()in theDogclass, you did not have to pass inmilo. ToString()but rather could just pass inmilo!
If you comment out the overridden function, the base method will be invoked. The base class default behavior is to return a string with the name of the class itself. Thus, the output would be changed to the meaningless:
My dog Milo weighs Dog pounds
Classes do not need to declare explicitly that they derive fromObject; the inheritance is implicit.
Next: Boxing and Unboxing Types >>
More C# Articles
More By O'Reilly Media
|
This article is excerpted from chapter 11 of Learning C# 2005, Second Edition, written by Jesse Liberty and Brian MacDonald (O'Reilly, 2006; ISBN: 0596102097). Check it out today at your favorite bookstore. Buy this book now.
|
|