Section 11.6. The Root of All Classes: Object

   

11.6 The Root of All Classes: Object

All VB.NET classes, of any type, are treated as if they ultimately derive from a single class, Object. Object is 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 VB.NET, the root class is Object. 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 and do override. These include Equals( ), which determines if two objects are the same, GetType( ), which returns the type of the object and ToString( ), 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 of Object.

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 15).

GetType( )

Provides access to the type object.

ToString( )

Provides a string representation of the object.

Finalize( )

Cleans up nonmemory resources; implemented by a destructor.

MemberwiseClone( )

Creates copies of the object; should never be implemented by your type.

ReferenceEquals( )

Evaluates whether two objects refer to the same instance.

In Example 11-4, the Dog class overrides the ToString( ) method inherited from Object, to return the weight of the Dog. This example also takes advantage of the startling fact that intrinsic types (Integer, Long, etc.) can also be treated as if they derive from Object, and thus you can call ToString( ) on an integer variable! Calling ToString( ) on an intrinsic type returns a string representation of the variable's value.

Example 11-4. Overriding ToString
 Option Strict On Imports System Public Class Dog     Private weight As Integer     ' constructor     Public Sub New(ByVal weight As Integer)         Me.weight = weight     End Sub 'New     ' override Object.ToString     Public Overrides Function ToString( ) As String         Return weight.ToString( )     End Function 'ToString End Class 'Dog Public Class Tester     Shared Sub Main( )         Dim i As Integer = 5         Console.WriteLine("The value of i is: {0}", i.ToString( ))         Dim milo As New Dog(62)         Console.WriteLine("My dog Milo weighs {0} pounds", milo.ToString( ))     End Sub 'Main End Class 'Tester 
  Output:  The value of i is: 5 My dog Milo weighs 62 pounds 

The documentation for Object.ToString( ) reveals its signature:

 Overridable Public Function ToString( ) As String 

It is an overridable public method that returns a string and that takes no parameters. All the built-in types, such as Integer, derive from Object and so can invoke Object's methods.

The Console class's Write( ) and WriteLine( ) methods call ToString( ) for you on objects that you pass in for display.

Example 11-4 overrides the Overridable ToString( ) function for Dog, so that calling ToString( ) on a Dog object will return a reasonable value. 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 from Object; the inheritance is implicit.

   


Learning Visual Basic. NET
Learning Visual Basic .Net
ISBN: 0596003862
EAN: 2147483647
Year: 2002
Pages: 153
Authors: Jesse Liberty

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net