Visual Basic: An Object Oriented Approach 10 - Polymorphism.

Slides:



Advertisements
Similar presentations
Classes, Exceptions, Collections, and Scrollable Controls
Advertisements

Chapter 10: Introduction to Inheritance
Visual Basic: An Object Oriented Approach 6: Object Modelling.
Wednesday, 10/2/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 10/2/02  QUESTIONS (on HW02 – due at 5 pm)??  Today:  Review of parameters  Introduction.
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA By Robert T. Grauer Maryann Barber.
Visual Basic Project Files:.VBP file: Project File: a small text file that contains the names of other files in the project, as well as some information.
Exploring Office Grauer and Barber 1 Creating More Powerful Applications: Introduction to VBA(Wk9)
Visual Basic: An Object Oriented Approach 3 – Making Objects Work.
Chapter 10 Classes Continued
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Microsoft Visual Basic 2005: Reloaded Second Edition
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Chapter 9: Getting Comfortable with Object- Oriented Programming.
Programming Pillars Introduction to Object- Oriented Programming.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
More on Hierarchies 1. When an object of a subclass is instantiated, is memory allocated for only the data members of the subclass or also for the members.
Tutorial 6 The Repetition Structure
Exploring Microsoft Access Chapter 8 Creating More Powerful Applications: Introduction to VBA.
Chapter 12 Support for Object oriented Programming.
Applications Development
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Salman Marvasti Sharif University of Technology Winter 2015.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
COPYRIGHT 2010: Dr. David Scanlan, CSUS OBJECTIVES: How to write classes How to create an object from a class using the "New" keyword. How to communicate.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
Introduction to Object-Oriented Programming Lesson 2.
Introduction to OOP CPS235: Introduction.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
31/01/ Selection If selection construct.
Controlling Program Flow with Decision Structures.
Object-Oriented Programming: Inheritance and Polymorphism.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CSCE 240 – Intro to Software Engineering Lecture 3.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
These materials where developed by Martin Schray
CS0004: Introduction to Programming
Visual Basic Fundamental Concepts
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Sections Inheritance and Abstract Classes
Visit for more Learning Resources
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 3: Using Methods, Classes, and Objects
Types of Programming Languages
Visual Basic..
Department Array in Visual Basic
Chapter 11: Classes, Instances, and Message-Handlers
CS285 Introduction - Visual Basic
Object-Oriented Programming: Inheritance and Polymorphism
CIS16 Application Development and Programming using Visual Basic.net
Chapter 9 Introduction To Classes
Presentation transcript:

Visual Basic: An Object Oriented Approach 10 - Polymorphism

Polymorphism The facility that permits objects of different classes to be interchangeable Different classes of object can have properties and methods that are compatible Same names Same parameters Same return types (for Functions or Property Gets) Considering only these methods, it is possible to send the same sequence of messages to objects of either class These classes are polymorphs

Polymorphism Example Sub ChangeObject(O As Object) O.Caption = “New Caption” O.BackColor = vbRed O.Width = 5000 O.Move 1000, 1000 End Sub This code can be applied to several different types of VB Controls CommandButtons Labels CheckBoxes Note parameter type – Object A generalism All types of VB object can be referred to by this type of object variable Controls User defined classes

More general – less secure We could also have sent a TextBox control to the example code shown This has no Caption property The code would cause an error that could only be detected at run-time Using Object as a general type should be considered bad practice Ideally, we should restrict the type to one that has all of the properties and methods used in the code Alternatively, we can check the object type before we access a property or method

Preventing Wrong-Type errors Sub ChangeObject(O As Object) If TypeOf(O) Is Label Or TypeOf (O) Is CommandButton Then O.Caption = “New Caption” ElseIf TypeOf(O) Is TextBox Then O.Text = “New Text” Else ‘ Does not have a caption or text property, best leave it alone End If ‘ ‘ ‘ ‘ End Sub This could get very tedious, and is prone to errors

Ideally we need Type-Safety Some way of stating that a class of objects is compatible with some general class Interfaces provide this security An interface is nothing more than a definition of Properties and Methods with no executable code involved Name of Property/Method Parameters (arguments) passed to them Values returned from them

Example of a Type Definition ‘ IData Interface class Public Sub StoreData(DataFile As Integer) End Sub Public Function LoadData(DataFile As _ Integer) End Function Public Property Get Data() As Variant End Property Public Property Set Data(NewValue As _ Variant) End Property Obvious feature – all procedures are empty Purely a statement of how a Property, Sub or Function would be called by other code A Message vocabulary This is placed in a class, but called an Interface

Type Declaration A class can declared so it Implements an Interface It can then be guaranteed to provide a set of Procedures/Methods There is no guarantee that these do anything sensible – just that they are compatible This is Interface Inheritance

e.g. An implementing class Implements IData ‘ This class must conform to IData Private … ‘ Declare member variables for class Public Sub IData_StoreData(DataFile As Integer) ‘ Does something to store information End Sub Public Function IData_ LoadData(DataFile As Integer) ‘ Does something to load information End Function Public Property Get IData_ Data() As Variant ‘ Returns an internal value End Property Public Property Set IData_ Data(NewValue As Variant) ‘ Updates an internal value End Property

Implementing class – key features VB Automatically provides templates of Properties and Methods to match interface No need to type (or mistype) Programmer is responsible for coding how a given class will implement a specific interface function Client program can use any class that implements the interface interchangeably

Interfaces Application Interchangeable Classes Interface Interface definitions are created to match an application Classes that implement the interface will ‘fit’ the application

Typical example of polymorphism Computer Aided Drawing (see chapter 10 Exercise) The program knows how to draw, move and edit an IShape object There are no IShape objects Several ‘concrete’ shape classes implement the IShape interface the drawing program uses The program can therefore draw, move etc. instances of these classes, even though there is no code in it that refers to them

CAD Example Private Sub DrawShapes() Dim I As IShape P.Cls For Each I In DrawList I.Draw P Next End Sub This is the abstract class, or superclass This is a collection of shape objects of various classes This statement draws a shape on PictureBox P, no matter what class it belongs to

CAD Program - result All of these are IShape objects, but are different classes