OOP Keywords
Defining a class
Section titled “Defining a class”Classes are vital aspects of OOP. A class is like the “blueprint” of an object. An object has the properties of a class, but the characteristics are not defined within the class itself. As each object can be different, they define their own characteristics.
Public Class PersonEnd Class
Public Class CustomerEnd ClassA class can also contain subclasses. A subclass inherits the same properties and behaviors as its parent class, but can have its own unique properties and classes.
Inheritance Modifiers (on classes)
Section titled “Inheritance Modifiers (on classes)”Inherits
Section titled “Inherits”Specifies the base (or parent) class
Public Class PersonEnd Class
Public Class Customer Inherits Person
End Class
'One line notationPublic Class Student : Inherits PersonEnd ClassPossible objects:
Dim p As New PersonDim c As New CustomerDim s As New StudentNotInheritable
Section titled “NotInheritable”Prevents programmers from using the class as a base class.
Public NotInheritable Class PersonEnd ClassPossible objects:
Dim p As New PersonMustInherit
Section titled “MustInherit”Specifies that the class is intended for use as a base class only. (Abstract class)
Public MustInherit Class PersonEnd Class
Public Class Customer Inherits PersonEnd ClassPossible objects:
Dim c As New CustomerInheritance Modifiers (on properties and methods)
Section titled “Inheritance Modifiers (on properties and methods)”Overridable
Section titled “Overridable”Allows a property or method in a class to be overridden in a derived class.
Public Class Person Public Overridable Sub DoSomething() Console.WriteLine("Person") End SubEnd ClassOverrides
Section titled “Overrides”Overrides an Overridable property or method defined in the base class.
Public Class Customer Inherits Person
'Base Class must be Overridable Public Overrides Sub DoSomething() Console.WriteLine("Customer") End SubEnd ClassNotOverridable
Section titled “NotOverridable”Prevents a property or method from being overridden in an inheriting class. Default behaviour. Can only be declared on overrides methods
Public Class Person
Public Overridable Sub DoSomething() Console.WriteLine("Person") End Sub
End Class
Public Class Customer Inherits Person
Public NotOverridable Overrides Sub DoSomething() Console.WriteLine("Customer") End Sub
End Class
Public Class DetailedCustomer Inherits Customer
'DoSomething can't be overriddenEnd ClassExample Usage:
Dim p As New Personp.DoSomething()
Dim c As New Customerc.DoSomething()
Dim d As New DetailedCustomerd.DoSomething()Output:
PersonCustomerCustomerMustOverride
Section titled “MustOverride”Requires that a derived class override the property or method.
MustOverride methods must be declared in MustInherit classes.
Public MustInherit Class Person
Public MustOverride Sub DoSomething() 'No method definition here
End Class
Public Class Customer Inherits Person
'DoSomething must be overridden Public Overrides Sub DoSomething() Console.WriteLine("Customer") End Sub
End ClassExample Usage:
Dim c As New Customerc.DoSomething()Output:
CustomerMyBase
Section titled “MyBase”The MyBase keyword behaves like an object variable that refers to the base class of the current instance of a class.
Public Class Person Public Sub DoSomething() Console.WriteLine("Person") End SubEnd Class
Public Class Customer Inherits Person
Public Sub DoSomethingElse() MyBase.DoSomething() End Sub
End ClassUsage example:
Dim p As New Personp.DoSomething()
Console.WriteLine("----")
Dim c As New Customerc.DoSomething()c.DoSomethingElse()Output:
Person----PersonPersonMe vs MyClass
Section titled “Me vs MyClass”Me uses the current object instance.
MyClass uses the memberdefinition in the class where the member is called
Class Person Public Overridable Sub DoSomething() Console.WriteLine("Person") End Sub
Public Sub useMe() Me.DoSomething() End Sub
Public Sub useMyClass() MyClass.DoSomething() End SubEnd Class
Class Customer Inherits Person
Public Overrides Sub DoSomething() Console.WriteLine("Customer") End SubEnd ClassExample Usage:
Dim c As New Customerc.useMe()c.useMyClass()Output:
CustomerPersonOverloading
Section titled “Overloading”Overloading is the creation of more than one procedure, instance constructor, or property in a class with the same name but different argument types.
Class Person Overloads Sub Display(ByVal theChar As Char) ' Add code that displays Char data. End Sub
Overloads Sub Display(ByVal theInteger As Integer) ' Add code that displays Integer data. End Sub
Overloads Sub Display(ByVal theDouble As Double) ' Add code that displays Double data. End SubEnd ClassShadows
Section titled “Shadows”It redeclares a member that is not overridable. Only calls to the instance will be affected. Code inside the base classes will not be affected by this.
Public Class Person Public Sub DoSomething() Console.WriteLine("Person") End Sub
Public Sub UseMe() Me.DoSomething() End SubEnd ClassPublic Class Customer Inherits Person Public Shadows Sub DoSomething() Console.WriteLine("Customer") End Sub
End ClassExample usage:
Dim p As New PersonDim c As New Customerp.UseMe()c.UseMe()Console.WriteLine("----")p.DoSomething()c.DoSomething()Output:
PersonPerson----PersonCustomerPitfalls:
Example1, Creating a new object through a generic. Which function will be used??
Public Sub CreateAndDoSomething(Of T As {Person, New})() Dim obj As New T obj.DoSomething()End Subexample usage:
Dim p As New Personp.DoSomething()Dim s As New Students.DoSomething()Console.WriteLine("----")CreateAndDoSomething(Of Person)()CreateAndDoSomething(Of Student)()Output: By intuition the result should be the same. Yet that is not true.
PersonStudent----PersonPersonExample 2:
Dim p As PersonDim s As New Studentp = sp.DoSomething()s.DoSomething()Output: By intuition you could think that p and s are equal and will behave equal. Yet that is not true.
PersonStudentIn this simple examples it is easy to learn the strange behaviour of Shadows. But in real-life it brings a lot of surprises. It is advisably to prevent the usage of shadows. One should use other alternatives as much as possible (overrides etc..)
Interfaces
Section titled “Interfaces”Public Interface IPerson Sub DoSomething()End Interface
Public Class Customer Implements IPerson Public Sub DoSomething() Implements IPerson.DoSomething Console.WriteLine("Customer") End Sub
End Class