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 - Lest we become Abstracted

DMarko1

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.

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...