Library tutorials & articles

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.

Comments

  1. 25 Jul 2006 at 13:14

    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 to a dll? I cannot do it on the server. Do I do it on my local computer and then upload?


    http://www.developerfusion.co.uk/show/4341/5/

  2. 22 Jun 2006 at 06:29

    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 therms and the class theory really feels like puting each part of the program into a separate box and then figuring out how to drill a way between them. I started to learn C++ OOP at least 20 time and after a couple of weeks I did not even had an idea what it is about (note: I am not a proffesional programmer)...

    I believe that: "The use of a programming language should be as simple as a pie and the algorithm should be the part where people spend the most of their time..."

    When I do C# programming I do all public and it works great for me. Simply keep it simple!!!

    Is there any web page or book where I could find how to do Non-OOP C# programming?

    Sincerely,

    Gabor Gorcsos

     

     

     

  3. 20 May 2005 at 23:21

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

  4. 22 Apr 2005 at 18:00

    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. - Building a Full-Featured Custom DataGrid Control. It's just one of those overlooked things. Oh well.

  5. 22 Apr 2005 at 17:26

    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 )
    What you presented in your article is you declared public property,then  made the (get,set) private)


    I have put this simple comparison code , so please advice your point, What is the logic behind reversing the modifier(public to private and vis versa).
    thanks
    Ehx
     
        // what is in the book (Begining Asp.Net Database using C# p 321)
           private string Country;
           public string Country
           {
               get{return _Country;}
               set{
    Country= value;}
           }


                  // what is in web article ( in your article)
           public string Name;
           private string _Name
           {
               get{return Name;}
               set{Name= value;}
           }

  6. 01 Jan 1999 at 00:00

    This thread is for discussions of The Quick & Dirty .NET Guide to C#/VB OOP.

Leave a comment

Sign in or Join us (it's free).

Dimitrios Markatos 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 deskto...

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Mar 25

    Hyper-V For Developers with Liam Westley

    Bristol, United Kingdom

    Liam Westley returns to Bristol fresh from his DDD8 exploits to give us a second dose of virtualization, this time focussing on Hyper-V.

Want to stay in touch with what's going on? Follow us on twitter!