Article Options
Premium Sponsor
Premium Sponsor

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

In its most simple terms, Inheritance in OOP describes the ability of one class to make use of the members of another class. Put another way, if Class B is said to inherit from Class A then Class A (the parent class) hands down its abilities to Class B (a child class).

You may sometimes see this parent/child relationship described as Base class and Derived class or Superclass and Subclass. People often talk about 'subclassing', when they mean they create a new child class, based on a parent. All these terms have the same meaning.

I will describe what inheritance means in more detail in a future article. For now though, just hold on to this general idea that the child inherits the Public members - Methods, Properties, etc - from the parent.

When it comes to inheritance in VB.NET, you could think of the System.Object class as being the father of all classes. What I mean by this is that the classes that are built in to the .NET Framework - Integer, DateTime, Size, StreamReader, etc - all inherit directly or indirectly from System.Object.

Classes that you create yourself, like the Person class, will also inherit directly from System.Object. So if System.Object has Public members (and it has) then these should be inherited by your Person Class (and they are).

At this stage we are only interested in one specific Public method - the ToString method. System.Object has a ToString method; therefore your Person class also has a ToString method.

If you use the parent class's ToString method as-is then what you will get is the functionality of the parent's method. That is, in this case, the functionality of System.Object's ToString method. As it happens, the ToString method of System.Object will simply return the name of the class.

So in our earlier example, that is exactly what happened. We used the ToString method to get some information about an instance of our class. As we haven't yet written our own version of ToString for the Person class, VB looked back up the inheritance chain and found the ToString method of the parent class.

As a result, as you saw on the previous page, this is what the ToString method produced:


This usually known as the fully qualified name of the class. "Fully Qualified" simple means that all the detail of the name is included - in this case, the namespace name (ClassBasics) and the Class name (Person).

We want to replace the instructions in the ToString method in the parent class with code that is more suitable for the purposes of a Person object. Replacing the instructions in this way is known as Overriding. More on this later.

First though, I want to take a quick tour of the Object Browser.

Sponsored Links