Microsoft VB 2005: Reloaded, Advanced - PowerPoint PPT Presentation

1 / 45
About This Presentation
Title:

Microsoft VB 2005: Reloaded, Advanced

Description:

Will be used by objects instantiated from the derived class ... Allows you to design a complete OO application using drag-and-drop techniques ... – PowerPoint PPT presentation

Number of Views:60
Avg rating:3.0/5.0
Slides: 46
Provided by: ftpC
Category:

less

Transcript and Presenter's Notes

Title: Microsoft VB 2005: Reloaded, Advanced


1
Microsoft VB 2005 Reloaded, Advanced
  • Chapter 4
  • Object Orientation Inheritance and Polymorphism

2
Objectives
  • 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

3
Objectives (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

4
Inheritance 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

5
An 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

6
An Inheritance Hierarchy for Predefined .NET
Framework Classes (continued)
7
The 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

8
Inheritance among Programmer-Defined Classes
  • Inheritance can prove equally helpful when you
    are creating your own classes within applications

9
Creating an Inheritance Relationship
  • When you create a new Windows Application project
    in Visual Studio 2005
  • The form that appears uses inheritance

10
Creating an Inheritance Relationship (continued)
11
Creating an Inheritance Relationship (continued)
12
Creating an Inheritance Relationship (continued)
13
More 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

14
More 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

15
More About Access Modifiers (continued)
16
Inheritance Overloading and Overriding
  • Objective
  • Understand how inheritance relates to the topics
    of overloading and overriding methods in Visual
    Basic

17
Overloaded 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

18
Overloaded Methods in the Same Class (continued)
19
Overloaded Methods in the Same Class (continued)
20
Overloaded Methods in the Same Class (continued)
21
Overloaded 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

22
Overriding 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

23
Overriding Methods (continued)
24
Using Inheritance in Applications
  • Objective
  • Implement inheritance in an application

25
Planning the College Personnel Application Using
UML
26
Implementing the College Personnel Application
27
Implementing the College Personnel Application
(continued)
28
Abstract Classes and Methods
  • Abstract classes and methods
  • Makes program code more organized and secure

29
Abstract 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

30
Abstract 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

31
Polymorphism 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

32
Visual 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

33
Adding a Class Diagram to an Existing Application
34
Adding a Class Diagram to an Existing Application
(continued)
35
Adding a Class Diagram to an Existing Application
(continued)
36
Adding a Class Diagram to an Existing Application
(continued)
37
Adding a Class Diagram to an Existing Application
(continued)
38
Adding a Class Diagram to an Existing Application
(continued)
39
Adding a Class with the Class Designer
40
Adding a Class with the Class Designer (continued)
41
Adding a Class with the Class Designer (continued)
42
Making 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

43
Summary
  • 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

44
Summary (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

45
Summary (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
Write a Comment
User Comments (0)
About PowerShow.com