1 / 10

More about classes and objects

More about classes and objects. Classes in Visual Basic.NET. 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. Shape. x, y : Integer.

galvin
Download Presentation

More about classes and objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 x, y : Integer draw() : String Rectangle Circle r : Integer h, w : Integer Shape, Circle and Rectangle

  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

More Related