Members

Technology Zones

Articles

Hosted By

MaximumASP

Info

Rated
Read 48,845 times

Contents

Related Categories

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

DMarko1

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.

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 desktop applications. Till now Jimmy has authored nearly two dozen .NET articles, published on Dot Net Junkies, 4 Guys From Rolla, Sitepoint, MSDN Academic Alliance, Developers.NET, The Official Microsoft ASP.NET Site, and here on Developer Fusion, covering various unique and advanced techniques on .NET.

Comments

  • Re: [4341] The Quick & Dirty .NET Guide to C#/VB OOP

    Posted by Yoenuts on 25 Jul 2006

    hello,


    I am very impressed by your tutorial as it finally allowed me to grasp the syntax behind OOP programming with .net.
    Only what I did not understand is how and where do I complile the .cs ...

  • Re: [4341] The Quick & Dirty .NET Guide to C#/VB OOP

    Posted by ggorcsos on 22 Jun 2006

    Honestly I believe that the basic idea of the OOP was really great, but to be able to use it one really has to have the head as a water melon. There is too much theory, too many ther...

  • Posted by James Crowley on 20 May 2005

    Though from personal experience I'd say 99.99% of the time, you'd want private member variables and public properties... ;)

  • Posted by DMarko1 on 22 Apr 2005

    Hi Ehx,

    That's true, and it's funny that in all my other articles I always write all private variables with public properties. i.e. - [url="http://www.developerfusion.co.uk/show/4676/1/"]Building a...

  • Confusion about this article (Get ,Set)

    Posted by aalhussein on 22 Apr 2005

    After reading your article, http://www.developerfusion.com/show/4341/6/
    I got realy confused!!

    from what I know from the book below, I declare private property, then declare public (get , set )
    W...