AddThis Social Bookmark Button

Print

Object Oriented Programming for VB.NET - Part 1
Pages: 1, 2, 3

Static Members

Looking at the Employee class in Listings 4, 5 and 6 again, you may wonder why we can use the Write method of the System.Console class without first instantiating a System.Console object. This is because in OOP languages there is a special type of member called a static member. The term shared is also used in VB.NET.



Static means the member is available without having to instantiate an object. For example, the class called SalaryLevel provides static fields only in Listing 7.

Listing 7: Static members in a class


Class SalaryLevel 
  Public Shared Level1 As Decimal = 35000
  Public Shared Level2 As Decimal = 40000
  Public Shared Level3 As Decimal = 45000
  Public Shared Level4 As Decimal = 50000
  Public Shared Level5 As Decimal = 55000
  Public Shared Level6 As Decimal = 60000
  Public Shared Level7 As Decimal = 65000
  Public Shared Level8 As Decimal = 70000
  Public Shared Level9 As Decimal = 75000
  Public Shared Level10 As Decimal = 80000
End Class

You can then use the class in your program, as illustrated in Listing 8.

Listing 8: Using a static member of a class


Imports System

Class SalaryLevel 
  Public Shared Level1 As Decimal = 35000
  Public Shared Level2 As Decimal = 40000
  Public Shared Level3 As Decimal = 45000
  Public Shared Level4 As Decimal = 50000
  Public Shared Level5 As Decimal = 55000
  Public Shared Level6 As Decimal = 60000
  Public Shared Level7 As Decimal = 65000
  Public Shared Level8 As Decimal = 70000
  Public Shared Level9 As Decimal = 75000
  Public Shared Level10 As Decimal = 80000
End Class

Class Employee

  Dim yearlyBonus As Decimal = 4000

  Public Sub PrintSalary()
    ' print the salary to the Console, use the static field of SalaryLevel
    Console.Write(SalaryLevel.Level4)
  End Sub

  Public Shared Sub Main()
    Dim employee As Employee
    employee = New Employee()
    employee.PrintSalary()
  End Sub

End Class

In the PrintSalary method of the Employee class, we can use the Level4 static field in the SalaryLevel class without first creating an object of type SalaryLevel. Members that are not static are called instance members.

Constructors

A constructor is a special method that must be present in a class for the class to get instantiated. In VB.NET, this method is called New. But, as you have seen, there is no New method in the classes in the previous code. That's right. When there is no constructor present, VB.NET will create one for you. When you instantiate an object by using the New keyword, the class's constructor is called. You can provide initialization code that is guaranteed to run when the object is instantiated. If you write a constructor explicitly in your class, VB.NET won't create it anymore.

Inheritance

Inheritance is a feature that allows you to extend a class. If you need some functionality, you can create a brand new class. If part of the functionality you need has been provided in a class written by someone else, however, you can then write a new class that extends the original class. Your class is called the child class or derived class, and the original class is called the parent class or the base class. The process of extending a class is called extension. Sometimes, the term subclass or inherit is used to describe the act of extending a class. In VB.NET a class can only extend one parent class. Multiple class inheritance is not allowed in VB.NET.

Syntactically, extending a class is done using a semicolon after the class name, followed by the word Inherits and the parent class name. For example, Listing 9 shows how we extend the class Employee to create a new class called Manager.

Listing 9: Extending a class


Imports System

Class Employee

  Dim salary As Decimal = 40000
  Dim yearlyBonus As Decimal = 4000

  Public Sub PrintSalary()
    ' print the salary to the Console
    Console.Write(salary)
  End Sub

End Class

Class Manager: Inherits Employee
End Class

If the word Inherits appears on the next line, you don't need the semicolon.


Class Manager
  Inherits Employee
End Class

Now, you can instantiate a Manager object and use members from the Employee class. The code is given in Listing 10. //////QUESTION: IMPORTS SYSTEM WAS STUCK IN THE ABOVE GRAF. IT PROBABLY BELONGS AS TEH FIRST LINE OF CODE. CHECK WITH AU.//////////

Listing 10: Instantiating a Manager object


Imports System

Class Employee

  Public salary As Decimal = 40000
  Public yearlyBonus As Decimal = 4000

  Public Sub PrintSalary()
    ' print the salary to the Console
    Console.Write(salary)
  End Sub

End Class

Class Manager: Inherits Employee
End Class

Module Module1
  Public Sub Main()
    Dim manager As Manager
    manager = New Manager()
    manager.PrintSalary()
  End Sub
End Module

The example in Listing 11 shows how we can extend the Manager class by writing a new method called PrintBonus.

Listing 11: Adding a method to the derived class


Class Manager: Inherits Employee
  Public Sub PrintBonus()
    Console.Write(yearlyBonus)
  End Sub
End Class

Note that member accessibility restriction applies. For example, you can't make the yearlyBonus field private, since this field is not accessible from the Manager class. Therefore, trying to compile the code will result in an error.

Inheritance is a common practice. In fact, the .NET Framework class library consist of hierarchies of classes that are derived from other classes. For example, the Button class in the Windows.Forms namespace is a child class of ButtonBase, which itself is a child of Control. All classes will eventually have the System.Object class as its root. System.Object is called the root or the superclass in the .NET Framework class library.

The power of inheritance can be demonstrated by the code in Listing 12.

Listing 12: Extending System.Windows.Forms.Form


Public Class MyForm : Inherits System.Windows.Forms.Form
End Class

This blank class declaration, when compiled and run, will display a Windows form. You can create a form without a single line of code. This is because MyForm inherits System.WIndows.Forms.Form. It inherits the functionality of the Form class.

NotInheritable Classes

You can prevent classes from being inherited by using the NotInheritable keyword. For example, the code in Listing 13 is a class called Calculator that cannot be inherited.

Listing 13: A non-inheritable class


NotInheritable Class Calculator
End Class

Trying to extend this class will raise a compile error. Why would you want to make your class non- inheritable? In some cases, you may not want other people to be able to extend your class. Another reason is that an inheritable class produces faster code. You should use inheritable classes with caution, however, because noninheritance defeats the purpose of OOP. Only use it if you are 100% sure that you will not extend the class. This type of class is also called a final class in some other OOP languages.

Part 2 of this series can be found here


Return to the .NET DevCenter.