Inheritance Ability to define a new class from

Inheritance • Ability to define a new class from an existing class • Purpose of Inheritance is reusability • For example, each form created is inherited from the existing Form class – Original class is called Base Class, Superclass, or Parent Class – Inherited class is called Subclass, Derived Class, or Child Class 1

Inheritance (cont. ) • Examine 1 st line of code for a form in the Editor Inherited Class, Derived Class, Subclass, Child Class Public Class Form 1 Inherits System. Windows. Form Base Class, Superclass, Parent Class 2

Aggregation-”Has a” • Sometimes a class refers to other objects that are essential components of the class and don’t have an independent existence Public Class Car ‘Properties, pieces of the car Dim mthe. Engine As Engine Dim m. Transmission As Transmission ‘more code… End Class Still a “has-a” relationship. A Transmission is not a Car, it is just part of a Car 3

Abstraction – “is-a” • Classes can be related in hierarchical ways – Animals: cats, dogs, birds – Vehicles: road vehicles – car, truck, bus; aircraft – helicopter, jet, blimp • Relationship is not the same as composition (“hasa”) – A cat “is-a” animal • Often, classes related this way have some behavior or state in common 4

Inheritance Example • Base Class Person – Person -Name -Address -Phone • Derived Classes – Employee – Customer – Student Employee Customer Student 5

Inheritance Implemented • New class can – Be based on another class (base class) – Inherit the properties and methods (but not constructors) of the base class, which can be • One of the VB existing classes • Your own class • Designate Inheritance by adding the Inherits statement referencing the base class 6

Polymorphism: Overriding and Overloading • Different classes of objects may have behaviors that are named the same but are implemented differently (Overriding) • An object of a certain class might have several behaviors associated that are named the same but have different signatures (different type-number of arguments) and different implementation. (Overloading) 7

Polymorphism Implemented • Overloading – Argument type-number (signature) determines which version of a method is used – Example: Message. Box. Show method • Overriding – Refers to a class that has the same method name as its base class – Method in subclass takes precedence 8

Overloading Implemented • Overloading means that two or more methods have the same name but a different list of arguments. • Create it by giving the same name to multiple procedures in your class module, each with a different argument list and a different implementation. 9

Overriding Methods • Methods created in subclass with the same name and the same argument list as the methods in the base class. • Subclass will use the method in its own class rather than that in the base class • To override a method – Declare the base class method with the Overridable keyword – Declare the subclass method with the Overrides keyword 10

Example : Representing Animals • Base Class Animal – Animal -Name -Age -Noise • Derived Classes – Dog – Cat – Cow Dog Cat Cow 11

Example : Representing Animals • Generic Animal Public Class Animal ‘properties, module level variables …. ‘Return the noise this animal makes Public Overridable Function noise( ) As String Return "? “ End Function End Class 12

• Specific Animals Cats Dogs Public Class Cat Inherits Animal ‘properties, module level variables …. ‘Return the noise cats makes Public Overrides Function noise( ) As String Return “Meow“ End Function End Class Public Class Dog Inherits Animal ‘properties, module level variables …. ‘Return the noise a dog makes Public Overrides Function noise( ) As String Return “Woof“ End Function End Class 13

My. Base Keyword My. Base is commonly used to access base methods that are overridden in a derived class. Public Class Animal Dim mstr. Message As String ‘Property message() As String ‘code …. End Property Public Overridable Function noise() As String Me. mstr. Message = "the noise is " Public Class Dog Inherits Animal Public Overrides Function noise() As String My. Base. noise() ‘My. Base takes all existing code in the ‘base class Return (Me. message & "Woof") End Function End Class 14

Creating a Base Class Strictly for Inheritance • Classes can be created strictly for inheritance and are never instantiated • Subclasses are created and instantiated which inherit the base class properties and methods • For such a base class include the Must. Inherit modifier on the class declaration 15

Object Browser • Use it to view the names, properties, methods, events and constants of VB objects, your own objects, and objects available from other applications • How to access it – Tab in Editor Window – View menu, Other Window, Object Browser 16

Opening Object Browser from Toolbar 17

Object Browser Objects list Namespace icons Browse list Find Symbol Member list Method icon Property icon Constants icon Event icon Class icon 18

Examining VB Classes Members of System. Windows. Forms. Message. Box Class 19

Examining VB Classes (cont. ) Display the Message. Box. Buttons Constants 20
- Slides: 20