Article Options
Recently Viewed
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 :

   


Person.ToString
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.71052631578948 out of 5
 38 people have rated this page
Article Score8033
Article Series
Comments    Submit Comment

Comment #1  (Posted by an unknown user on 08/12/2009)
Rating
Simple concept but way too long for such a simple execution.
 
Comment #2  (Posted by an unknown user on 08/29/2009)
Rating
Very Helpful! I got a lot from the extra explanation details.
Thank you Mr Mead
 
Comment #3  (Posted by an unknown user on 09/03/2009)
Rating
I figure that this article may be too long for some, but I found it very clearly written and fully informative.
 
Comment #4  (Posted by Steve on 10/16/2009)
Rating
Excellent article - the length is irrelevant, he explained something in a very easy-to-understand way. It took me about 25 minutes to read this article and I learned more in that time than in the previous 6 months from other tutorials!! Thanks Ged
 
Comment #5  (Posted by an unknown user on 11/13/2009)
Rating
Some superb information there Ged.. I have been in development for 7 years or so and still love to read experts tutorials like yours.. Excellent work
 
Comment #6  (Posted by an unknown user on 12/13/2009)
Rating
Very well explained. You take nothing for granted.
 
Comment #7  (Posted by oem software on 02/10/2012)
Rating
qCmsFl Good! Wish everybody wrote so:DD
 
Comment #8  (Posted by Buy oem Software on 02/10/2012)
Rating
R3iAUO 52. "The road will be overcome by that person, who goes." I wish you never stopped and be creative - forever..!!
 
Comment #9  (Posted by buy cheap oem software on 02/11/2012)
Rating
mFP1kj Interesting. We are waiting for new messages on the same topic!!....
 
Comment #10  (Posted by buy cheap oem software on 02/12/2012)
Rating
QsG3rO Hello! Read the pages not for the first day. Yes, the connection speed is not good. How can I subscribe? I would like to read you in the future!....
 
Comment #11  (Posted by Signal Peak Energy on 02/21/2012)
Rating
Gu2der Interesting, but still I would like to know more about it. Liked the article:D
 
Comment #12  (Posted by Above Ground Swimming Pool????? on 02/24/2012)
Rating
It`s really useful! Looking through the Internet you can mostly observe watered down information, something like bla bla bla, but not here to my deep surprise. It makes me happy..!
 
Comment #13  (Posted by Hook And Tackle on 02/24/2012)
Rating
Good day! I do not see the conditions of using the information. May I copy the text from here on my site if you leave a link to this page?!...
 
Comment #14  (Posted by Wedding Invitation on 02/24/2012)
Rating
Thank you very much! I took it for myself too. Will be useful!!...
 
Comment #15  (Posted by Mobile Marketing on 02/24/2012)
Rating
Yeah, it is clear now !... From the very beginning I did not understand where was the connection with the title !!...
 
Comment #16  (Posted by Rice Salad on 02/24/2012)
Rating
Is anybody strong in radio here? We need a colleague who would tell us briefly about the transistor T2. I hope there are radio amateurs here. If it`s not on the subject at all, then I`m sorry. I have to write because I have no choice. PS: if the spelling is not right then also I'm sorry, I'm just 13 years old!...
 
Comment #17  (Posted by Avocado Salad on 02/24/2012)
Rating
The material is on the five plus. But there is a minus! My internet speed 56kb/sek. The page was loading for about 40 seconds!...
 
Comment #18  (Posted by Maneesh Massey on 03/04/2012)
Rating
EXCELLENT !!! NUMBER ONE IN THE WHOLE WORLD !!
 
Comment #19  (Posted by Microsoft OEM Software on 03/07/2012)
Rating
wfbPE6 Enjoyed every bit of your blog.Really looking forward to read more. Keep writing.
 
Comment #20  (Posted by Discount OEM Software on 03/07/2012)
Rating
Qsgfmq Very informative blog.Really looking forward to read more. Keep writing.
 
Comment #21  (Posted by Microsoft OEM Software on 03/07/2012)
Rating
O6wpin I cannot thank you enough for the blog article.Thanks Again.
 
Comment #22  (Posted by Microsoft OEM Software on 03/07/2012)
Rating
4812wK wow, awesome article post.Much thanks again. Much obliged.
 
Comment #23  (Posted by Adobe OEM Software on 03/08/2012)
Rating
MFMPZq Im obliged for the article post. Great.
 
Comment #24  (Posted by elitemdspa on 03/08/2012)
Rating
Yeah, it is clear now !... From the very beginning I did not understand where was the connection with the title !!...
 
Comment #25  (Posted by windups on 03/08/2012)
Rating
Cool:) I would say say it exploded my brain..!
 
Comment #26  (Posted by web design palm city florida on 03/09/2012)
Rating
Hooray! the one who wrote is a cool guy..!
 
Comment #27  (Posted by wholesale men clothing on 03/20/2012)
Rating
Great, thanks for sharing this blog.Much thanks again. Want more.
 
Comment #28  (Posted by Bristol Airport Hotels on 04/19/2012)
Rating
xPsxxH Im obliged for the article.Really looking forward to read more. Keep writing.
 
Sponsored Links