Presentation is loading. Please wait.

Presentation is loading. Please wait.

More about classes and objects Classes in Visual Basic.NET.

Similar presentations


Presentation on theme: "More about classes and objects Classes in Visual Basic.NET."— Presentation transcript:

1 More about classes and objects Classes in Visual Basic.NET

2 We have seen... Basics of OOP terminology Object Class Method Overloading Overriding Basics of OOP concepts Abstraction Polymorphism Encapsulation Inheritance How to draw class diagrams

3 Shape, Circle and Rectangle Shape x, y : Integer draw() : String Circle r : Integer Rectangle h, w : Integer

4 Creating classes in VB.NET Classes are written in files, in a similar way than forms. They are added using the Add class... item in the Project menu. Involves... setting up the variables implementing a constructor implementing the methods required

5 Code for Shape Public Class Shape Protected x, y As Integer Public Sub New() x = 12 y = 30 End Sub Public Function draw() draw = "I am a nice shape" End Function End Class

6 Abstract classes Cannot be instantiated Only classes that extend them can be instantiated Used as holders for their subclasses VB.NET uses keyword "MustInherit" Public MustInherit Class Shape

7 Creating subclasses Two options: New file (through Add class...) Same file Subclasses might hold variables of their own VB.NET uses keyword "Inherits" Public Class Circle Inherits Shape

8 Overriding methods A subclass can redefine methods that appear in its parent Circle can give a new implementation to inherited method draw() VB.NET uses the keyword "Overrides" Public Overrides Function draw()

9 Abstract methods Sometimes the parent class just "mentions" a method, without providing an implementation VB.NET uses keyword "MustOverride" Any class with at least one abstract method must be declared abstract It is a good idea to make the method draw() in Shape abstract

10 Abstract Shape Public MustInherit Class Shape Protected x, y As Integer Public Sub New() x = 12 y = 42 End Sub Public MustOverride Function draw() End Class


Download ppt "More about classes and objects Classes in Visual Basic.NET."

Similar presentations


Ads by Google