Title: 110J1
1Inheritance
- 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
2Inheritance (cont.)
- Examine 1st line of code for a form in the Editor
Inherited Class, Derived Class, Subclass, Child
Class
Public Class Form1 Inherits System.Windows.Forms.
Form
Base Class, Superclass, Parent Class
3Aggregation-Has a
- Sometimes a class refers to other objects that
are essential components of the class and dont
have an independent existence - Public Class Car
- Properties, pieces of the car
- Dim mtheEngine As Engine
- Dim mTransmission As Transmission
- more code
- End Class
- Still a has-a relationship.
- A Transmission is not a Car, it is just part of a
Car
4Abstraction 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
(has-a) - A cat is-a animal
- Often, classes related this way have some
behavior or state in common
5Inheritance Example
- Base Class
- Person
- Derived Classes
- Employee
- Customer
- Student
6Inheritance 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
7Polymorphism 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.(Overlo
ading)
8Polymorphism Implemented
- Overloading
- Argument type-number (signature) determines which
version of a method is used - Example MessageBox.Show method
- Overriding
- Refers to a class that has the same method name
as its base class - Method in subclass takes precedence
9Overloading 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.
10Overriding 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
11Example Representing Animals
- Base Class
- Animal
- Derived Classes
- Dog
- Cat
- Cow
12Example 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
13 Specific Animals
- Cats
- 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
- Dogs
- 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
14MyBase Keyword
MyBase is commonly used to access base methods
that are overridden in a derived class.
Public Class Animal Dim mstrMessage As
String Property Property message() As
String code . End Property Public
Overridable Function noise() As
String Me.mstrMessage "the noise is "
End Function End Class
Public Class Dog Inherits Animal Public
Overrides Function noise() As String
MyBase.noise() MyBase takes all existing
code in the base class Return
(Me.message "Woof") End Function End Class
15Creating 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 MustInherit
modifier on the class declaration
16Object 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
17Opening Object Browser from Toolbar
18Object Browser
19Examining VB Classes
Members of System.Windows.Forms.MessageBox Class
20Examining VB Classes (cont.)
Display the MessageBoxButtons Constants