Article Options
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  Object Oriented Programming Basics - Methods  »  Overridable and Overrides
Object Oriented Programming Basics - Methods
by Ged Mead | Published  08/11/2009 | .NET Newbie | Rating:
Ged Mead

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.


View all articles by Ged Mead...
Overridable and Overrides

Overridable and Overrides

Although Overridable and Overrides sounds quite technical, it isn't really because the meaning is just as you would use the words in normal English. That is, the one version will override or replace the settings of the original or earlier version. If something is Overridable, it can be replaced.

You are allowed to override a method in the parent class if that class has been made Overridable. Also it is important to remember that the signature of the overridden method must be the same as the signature of the parent method. ("Signature" was covered in an earlier article)

So if the parent class contains an Overridable method, then the child class may override that method. The developer of the child class has the choice as to whether to provide an overridden method or not. It is not mandatory.

The System.Object.ToString is designated as Overridable as we saw in the Object Browser :


You can create any String you like as the returned result of the ToString method. In the case of the Person Class, the Forename and Surname are possibly the two most useful pieces of core information. So I will use these as the returned value of the ToString function - that is, what the client code gets if it calls this method.

Here is that ToString method for the Person Class:

Code Copy
    Public Overrides Function ToString() As String
        Return m_forename & " " & m_surname
    End Function

It finds the value of the m_forename and m_surname for the current instance and returns those values as a concatenated string, with a space between the two parts. Concatenated is simply developer-speak for "joined together".

Testing out this method, place the following code in the Button click event of Form1 in the ClassBasics project:

Code Copy
        ' Create a new Person Instance
        Dim RealPerson As New Person("Ged", "Mead")
        '  Display using the default ToString method
        Label1.Text = RealPerson.ToString

This time, you will get the result you want:

In this article I covered one method, ToString, in some depth. You saw that all classes inherit from System.Object and that we can override the ToString method in the System.Object class. We can do this because the declaration of the System Object's ToString method includes the Overridable modifier.

If you choose to override a method there are two key requirements:

  1. You must include the Overrides modifier in the child method declaration
  2. The signature must be the same as that of the parent method.

You can get a lot of very useful information about classes and hierarchies by using the Object Browser tool in Visual Studio.

Of course, you can include many other methods in a class - and probably would. We will look at some other methods in a later article.

In the next article in this series, I will look at Properties in more detail, how to validate values and send messages back to client code.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:3.38271604938272 out of 5
 81 people have rated this page
Article Score38531
Sponsored Links