Title: Microsoft VB 2005: Reloaded, Advanced
1Microsoft VB 2005 Reloaded, Advanced
- Chapter 4
- Object Orientation Inheritance and Polymorphism
2Objectives
- Explain how Visual Basic predefined classes use
inheritance - Create overloaded methods in a class
- Override methods in a base class with methods in
a derived class
3Objectives (continued)
- Create Visual Basic classes that have inheritance
relationships - Create abstract classes and methods in an
application - Use overtyped variables through polymorphism
- Use the Class Designer to create and modify
applications
4Inheritance among Visual Basic Predefined Classes
- Inheritance
- Special relationship between two classes
- Base class gives the derived class direct access
to all of its Public (not Private) members - Idea behind inheritance
- Promote code reuse
5An Inheritance Hierarchy for Predefined .NET
Framework Classes
- Inheritance hierarchy
- Treelike structure showing how classes inherit
the members of other classes - MarshalByRefObject
- Required for objects in one application domain to
communicate with objects in another application
domain using object references
6An Inheritance Hierarchy for Predefined .NET
Framework Classes (continued)
7The Is-A Class Relationship
- Inheritance is called an is-a relationship
- Instance of a derived class is a (or rather, is
an) instance of any of its base classes
8Inheritance among Programmer-Defined Classes
- Inheritance can prove equally helpful when you
are creating your own classes within applications
9Creating an Inheritance Relationship
- When you create a new Windows Application project
in Visual Studio 2005 - The form that appears uses inheritance
10Creating an Inheritance Relationship (continued)
11Creating an Inheritance Relationship (continued)
12Creating an Inheritance Relationship (continued)
13More About Access Modifiers
- Access modifiers
- Public
- No restrictions on access to a class member
- Private
- Class members can be directly accessed only
within the class in which the members are
declared - Friend
- Class member can be directly accessed from
anywhere in the application in which it is
defined - Intra-application access
14More About Access Modifiers (continued)
- Access modifiers (continued)
- Protected
- Member is directly accessible within the
declaring class and classes that inherit from the
declaring class - Protected Friend
- Grants access anywhere within an application and
within any derived classes - Principle of least privilege
- Elements in a computing environment should be
granted only information and resources that are
immediately necessary
15More About Access Modifiers (continued)
16Inheritance Overloading and Overriding
- Objective
- Understand how inheritance relates to the topics
of overloading and overriding methods in Visual
Basic
17Overloaded Methods in the Same Class
- Overloaded method
- One of two or more methods in the same class or
inheritance hierarchy with the same name - But an otherwise different method signature
- Method signature
- Combination of method name and its parameter list
- If two methods have the same name but different
signatures - They must have different parameter lists
18Overloaded Methods in the Same Class (continued)
19Overloaded Methods in the Same Class (continued)
20Overloaded Methods in the Same Class (continued)
21Overloaded Methods in a Derived Class
- Within an inheritance hierarchy of classes
- An overloaded method in a derived class must use
the keyword Overloads in its declaration
22Overriding Methods
- Overridden method
- A method in a base class with exactly the same
signature as a method in a derived class - Method in derived class has precedence
- Will be used by objects instantiated from the
derived class - Objects instantiated from the base class will use
the method in the base class - Example
- ToString() method
23Overriding Methods (continued)
24Using Inheritance in Applications
- Objective
- Implement inheritance in an application
25Planning the College Personnel Application Using
UML
26Implementing the College Personnel Application
27Implementing the College Personnel Application
(continued)
28Abstract Classes and Methods
- Abstract classes and methods
- Makes program code more organized and secure
29Abstract Classes
- Abstract class
- Class with data members and/or method members
from which objects cannot be instantiated - Container for common sets of data members and
method members - That other object-defining classes can inherit
and implement - Concrete class
- Class from which objects can be instantiated
- Declaring an abstract class example
- Public MustInherit Class Person
30Abstract Methods
- Abstract method
- Declared in a base class but has no body or
implementation in that base class - Must be implemented in all derived classes
- Implementations can be different in each class
- Declaring abstract methods
- Use keyword MustOverride in base class
- Use keyword Overrides in derived class
31Polymorphism and Overtyping
- Polymorphism literally means many forms
- Polymorphism
- Overloading and overriding are both examples of
polymorphism - Overtyping is another example of polymorphism
- Overtyping
- Variable with the data type of a base class can
refer to a variable with the data type of a
derived class
32Visual Studio 2005 Class Designer
- Class Designer
- Visual design environment for the Common Language
Runtime - Allows you to design a complete OO application
using drag-and-drop techniques - Also provides a visual representation of an OO
application by creating a class diagram - Makes it easier for you to edit existing code and
communicate your application design to others
33Adding a Class Diagram to an Existing Application
34Adding a Class Diagram to an Existing Application
(continued)
35Adding a Class Diagram to an Existing Application
(continued)
36Adding a Class Diagram to an Existing Application
(continued)
37Adding a Class Diagram to an Existing Application
(continued)
38Adding a Class Diagram to an Existing Application
(continued)
39Adding a Class with the Class Designer
40Adding a Class with the Class Designer (continued)
41Adding a Class with the Class Designer (continued)
42Making Final Modifications to the Application
- After using Class Designer
- Programmer still needs to provide details of
writing the code necessary to make application
work as planned
43Summary
- Inheritance
- Special relationship between two classes in which
the base class gives to the derived class direct
access to all of its Public (not Private) data
members and method members - Called an is-a relationship between classes
- Inheritance hierarchy
- Treelike structure showing how classes inherit
the members of other classes - Create an inheritance relationship using the
keyword Inherits
44Summary (continued)
- Access modifiers Public, Private, Friend,
Protected, Protected Friend - An overloaded method is one of two or more
methods in the same class or inheritance
hierarchy with the same name but different
signatures - An overridden method is a method in a base class
with same signature as a method in a derived
class - Adding classes to an existing applications
- An abstract class is a class from which objects
cannot be instantiated
45Summary (continued)
- An abstract method
- Method that is declared in a base class but that
has no body or implementation in that base class - Polymorphism usually implies the ability of a
variable with the data type of a base class to
refer to a variable with the data type of a
derived class - Visual Studio 2005 Class Designer
- Allows you to create a visual representation of
an object-oriented application