Library tutorials & articles

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

Lest we become Abstracted

Huh? I thought I'd be innovative. Well, let's now look into another type of class, although nothing new to VB - Abstract classes. These are classes that act like generic, nonfunctional templates, on the surface similar to Interfaces , for more specialized classes that could contain both non-abstract and abstract methods or properties, etc., intended for a new, more robust class. Their selling point is their ability to remain silent, minimally predisposed and standing by ready to be redefined and implemented once inherited, and further help in pinpointing any runtime compilation or implementation errors.

Both abstract classes and interfaces cannot be instantiated. As we'll examine later, interfaces are contracts for other classes behavior with no set functionality. Although similar in concept, abstract classes however can contain abstract functionality (methods, etc.), as well as typical non-abstract methods.

Therefore, creating one take's place when you use the keyword abstract as you declare the class. When inheriting an abstract class in a regular or non-abstract class, you need to override all methods or properties before implementing, unless they are not abstract members. When implementing non-abstract members inherited from an abstract class in a non-abstract class you need to use the keyword Shadows in VB and new in C#. This behavior is known as method hiding .

Abstract classes are more useful when multiple versions of a component are required and for more larger sets of code, as opposed to an interface with its purpose aimed at smaller, unrelated bits of functionality. Remember the Class Vs Struct comparison? Kind of like that. Moreover, abstract classes could also inherit from non-abstract classes and even interfaces!

This is a basis for polymorphism as we'll later examine. However not quite in this context, since we're not altering it base form to another form.

Below we create an abstract House class with abstract members. Upon creating a new non-abstract class we inherit our abstract class and implement its features by overriding them, like so:

[C#]
public abstract class House
{
  public abstract string ShowColor();
  public abstract string ShowSize();
  //Non-Abstract method
  public string Whatever(){
    return "Anything";
  }
}
public class HouseStats : House
{
  public override string ShowColor()
  {
    return "blue";
  }
  public override string ShowSize()
  {
    return "small";
  }
  //Method Hiding
  public new string Whatever(){
    return "New Value";
  }

}

[VB]
Public MustInherit Class House

  MustOverride Function ShowColor()
  'Non-Abstract Method
  Public Function Whatever() As String
    Return "Anything"
  End Function

End Class

Public Class HouseStats : Inherits House

  Overrides Function ShowColor()
    Return "blue"
  End Function
  Public Shadows Function Whatever() As String
    Return "New Value"
  End Function

End Class

As you can tell, the C# keyword abstract translated to MustInherit in VB, in turn defining these types of classes and non-instantiable base classes.

Consequently, because of its design, abstract classes, and interfaces as we'll later examine, work well avoiding run-time bugs associated with derived classes, due to their nature in being overridden when used.

AddThis

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;}
           }

Leave a comment

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

Related discussion

Related jobs

Events coming up

  • Oct 14

    What’s New in Visual Studio 2008 Service Pack 1?

    Birmingham, United Kingdom

    “Service Pack? We’re calling it a Service Pack? Are you kidding??!?!” Visual Studio 2008 Service Pack 1 will release later in 2008 alongside .NET Framework V3.5 Service Pack 1 and, together, they represent a significant upgrade to Visual Studio 2008. There are enhancements across many areas of the .NET Framework such as data access, windows application development and web development and there are also corresponding changes in the development environment to support the new framework features.