The Quick & Dirty .NET Guide to C#/VB OOP

Inheritance

Inheritance in OOP is simply taking features from one object and implementing them in another one, like having a new super-duper object, with your new class being a subclass of, and inheriting from, the derived base or main class. Inheritance plays a vital role when attempting to create new custom controls in .NET. For example when creating a Composite Control (made up of multiple server controls) it is formed through inheriting all the features found within the base Class Control .

In any event, let's work with our house class in creating a new class called Location, that will simply show us where our house is located. I'll inherit all the features of our derived house class and then obtain the same output in addition to our location from the code below:

[C#]
public class House {
  // Code same as above
}
public class Location : House {
  public string ShwLocation;
  private string Loc {
    get {return ShwLocation;}
    set {ShwLocation = value;}
  }
  public string ShowStats(){
    return base.Summary() + " It is located in " + Loc;
  }
}
[VB]
'Inheritance syntax in VB
Public Class Location : Inherits House

'or alternatively
Public Class Location
  Inherits House
  ' Rest of code here as above
End Class

OK let's examine what's taking place. We've created a new class Location, right in the same .cs source file with our House class, that inherited all the features (fields, methods, etc.) from our derived base class House. We set up our public ShwLocation field and our private Loc property to get and set our value. Next we create a new method ShowStats() and within it use base.Summary() to pull in the Summary() method's results from our inherited main class, and thus our new ShowStats() method's combined results as seen below.

And our .NET page now has this:

Location oHouse = new Location();
oHouse.HseBuilder = "Peter";
oHouse.HseSize = "looks OK";
oHouse.HseColor = "red";
oHouse.ShwLocation = "New York";

Response.Write (oHouse.ShowStats());

Here we create a new Location object and we use fields that are not in our Location object, but are in our House object! We've inherited all its features and added the ShwLocation only, and this is one not found in our base class but our new subclass . That's inheritance.

Your result:

Pretty nice. Now wouldn't it be even nicer still if we could inherit from multiple classes? Sure would, but only C++ gives you that luxury as of late. However, interfaces, as we'll look over a little later, are just that luxury to make up for it.

You might also like...

Comments

About the author

Dimitrios Markatos

Dimitrios Markatos United States

Dimitrios, or Jimmy as his friends call him, is a .NET developer/architect who specializes in Microsoft Technologies for creating high-performance and scalable data-driven enterprise Web and des...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“A computer lets you make more mistakes faster than any other invention in human history, with the possible exceptions of handguns and tequila” - Mitch Ratcliffe