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.

Parameters &

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.

“The most exciting phrase to hear in science, the one that heralds new discoveries, is not 'Eureka!' but 'That's funny...'” - Isaac Asimov