Microsoft Visual Basic 2005: Reloaded

Similar documents
Chapter 12. OOP: Creating Object- Oriented Programs. McGraw-Hill. Copyright 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.

Chapter 02 Building Multitier Programs with Classes

9/21/2010. Based on Chapter 2 in Advanced Programming Using Visual Basic.NET by Bradley and Millspaugh

CIS Intro to Programming in C#

Chapter 2. Building Multitier Programs with Classes The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Advanced Programming Using Visual Basic 2008

Microsoft Visual Basic 2005: Reloaded

Chapter 5 Object-Oriented Programming

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

Programming II (CS300)

Understanding Inheritance and Interfaces

CLASSES AND OBJECTS IN JAVA

Building Multitier Programs with Classes

Chapter 12. OOP: Creating Object-Oriented Programs The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Introduction to Programming Using Java (98-388)

Programming II (CS300)

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

Public Class ClassName (naming: CPoint)

Introduction to OOP. Procedural Programming sequence of statements to solve a problem.

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

PROGRAMMING LANGUAGE 2

Inheritance and Polymorphism

Ch02. True/False Indicate whether the statement is true or false.

CMPT 117: Tutorial 1. Craig Thompson. 12 January 2009

C++ Important Questions with Answers

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE. What is Inheritance?

CIS 209 Final Exam. 1. A Public Property procedure creates a property that is visible to any application that contains an instance of the class.

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

02 Features of C#, Part 1. Jerry Nixon Microsoft Developer Evangelist Daren May President & Co-founder, Crank211

Practice for Chapter 11

Lecture 10 OOP and VB.Net

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS608 Lecture Notes. Visual Basic.NET Programming. Object-Oriented Programming Inheritance (Part II) (Part II of II) (Lecture Notes 3B)

Visual Basic 2008 The programming part

Framework Fundamentals

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.


CS313D: ADVANCED PROGRAMMING LANGUAGE

OVERRIDING. 7/11/2015 Budditha Hettige 82

What is Inheritance?

Inheritance and Encapsulation. Amit Gupta

Data Structures (list, dictionary, tuples, sets, strings)

CS111: PROGRAMMING LANGUAGE II. Lecture 1: Introduction to classes

INHERITANCE & POLYMORPHISM. INTRODUCTION IB DP Computer science Standard Level ICS3U. INTRODUCTION IB DP Computer science Standard Level ICS3U

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

Introduction. In this preliminary chapter, we introduce a couple of topics we ll be using DEVELOPING CLASSES

Object Oriented Programming. C++ 6 th Sem, A Div Ms. Mouna M. Naravani

CS313D: ADVANCED PROGRAMMING LANGUAGE

Object-Oriented Programming Concepts

Object Oriented Programming. Java-Lecture 11 Polymorphism

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

POLYTECHNIC OF NAMIBIA SCHOOL OF COMPUTING AND INFORMATICS DEPARTMENT OF COMPUTER SCIENCE

Inheritance, and Polymorphism.

C++ Inheritance and Encapsulation

INHERITANCE: EXTENDING CLASSES

Chapter 14 Abstract Classes and Interfaces

And Even More and More C++ Fundamentals of Computer Science

Object-Oriented Programming (OOP) Fundamental Principles of OOP

1. A Web Form created in Visual Basic can only be displayed in Internet Explorer. True False

More C++ : Vectors, Classes, Inheritance, Templates

Object-Oriented Programming

I101 Introduction to Informatics

More C++ : Vectors, Classes, Inheritance, Templates. with content from cplusplus.com, codeguru.com

(12-1) OOP: Polymorphism in C++ D & D Chapter 12. Instructor - Andrew S. O Fallon CptS 122 (April 3, 2019) Washington State University

Questions Answer Key Questions Answer Key Questions Answer Key

MaanavaN.Com CS1203 OBJECT ORIENTED PROGRAMMING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Logistics. Final Exam on Friday at 3pm in CHEM 102

Object Oriented Features. Inheritance. Inheritance. CS257 Computer Science I Kevin Sahr, PhD. Lecture 10: Inheritance

Polymorphism. Hsuan-Tien Lin. OOP Class, April 8, Department of CSIE, NTU. H.-T. Lin (NTU CSIE) Polymorphism OOP 04/08/ / 26

What are the characteristics of Object Oriented programming language?

CS313D: ADVANCED PROGRAMMING LANGUAGE

ECE 122. Engineering Problem Solving with Java

I BCS-031 BACHELOR OF COMPUTER APPLICATIONS (BCA) (Revised) Term-End Examination. June, 2015 BCS-031 : PROGRAMMING IN C ++

Oops known as object-oriented programming language system is the main feature of C# which further support the major features of oops including:

Object Oriented Programming With Visual Basic.NET

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Mobile Application Programming. Swift Classes

Lecture 7: Classes and Objects CS2301

STUDENT LESSON A20 Inheritance, Polymorphism, and Abstract Classes

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

VALLIAMMAI ENGINEERING COLLEGE

Inheritance -- Introduction

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

(11-1) OOP: Inheritance in C++ D & D Chapter 11. Instructor - Andrew S. O Fallon CptS 122 (October 29, 2018) Washington State University

The major elements of the object-oriented model

JAVA MOCK TEST JAVA MOCK TEST II


CS111: PROGRAMMING LANGUAGE II

PASCAL - OBJECT ORIENTED

1 Developments in the Field of Control Engineering

Polymorphism. Zimmer CSCI 330

Factory Method. Comp435 Object-Oriented Design. Factory Method. Factory Method. Factory Method. Factory Method. Computer Science PSU HBG.

Uka Tarsadia University MCA ( 3rd Semester)/M.Sc.(CA) (1st Semester) Course : / Visual Programming Question Bank

Transcription:

Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 10 Creating Classes and Objects

Objectives After studying this chapter, you should be able to: Define a class Instantiate an object from a class that you define Add Property procedures to a class Include data validation in a class Create default and parameterized constructors Microsoft Visual Basic 2005: Reloaded, Second Edition 2

Objectives (continued) Include methods in a class Overload the methods in a class Create a derived class using inheritance Microsoft Visual Basic 2005: Reloaded, Second Edition 3

Classes and Objects Object-oriented programs are based on objects that are instantiated (created) from classes Properties: attributes that describe the object Methods: behaviors that allow the object to perform tasks A class encapsulates properties and methods VB.Net has many built-in classes You can define your own classes Microsoft Visual Basic 2005: Reloaded, Second Edition 4

Defining a Class Class statement: defines a class Use Pascal casing for the class name Define attributes and behaviors of the class within the class Code editor automatically adds the Class statement Microsoft Visual Basic 2005: Reloaded, Second Edition 5

Defining a Class (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 6

Defining a Class (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 7

Defining a Class (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 8

Defining a Class (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 9

Defining a Class (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 10

Example 1 Using a Class that Contains Public Variables Only Microsoft Visual Basic 2005: Reloaded, Second Edition 11

Using a Class that Contains Public Variables Only (continued) Any class variable declared with Public keyword can be accessed by any application that contains an instance of the class Use Pascal case for Public variables in a class Microsoft Visual Basic 2005: Reloaded, Second Edition 12

Using a Class that Contains Public Variables Only (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 13

Using a Class that Contains Public Variables Only (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 14

Using a Class that Contains Public Variables Only (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 15

Using a Class that Contains Public Variables Only (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 16

Using a Class that Contains Public Variables Only (continued) Access an object s attributes using the dot operator: objectvariable.attribute Disadvantages of Public variables in a class: The class cannot control the values assigned to the variables Violates the concept of OOP encapsulation Microsoft Visual Basic 2005: Reloaded, Second Edition 17

Example 2 Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods Microsoft Visual Basic 2005: Reloaded, Second Edition 18

Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods (continued) Class variables declared with Private keyword: Can only be used by the class Are hidden from the rest of the application Names should start with underscore _ Application can change class variable values only by using the class s methods Microsoft Visual Basic 2005: Reloaded, Second Edition 19

Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods (continued) Property procedure: A Public method in the class for manipulating a class variable Exposes a Private class variable as a Property for use by the application Microsoft Visual Basic 2005: Reloaded, Second Edition 20

Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 21

Using a Class that Contains a Private Variable, a Property Procedure, and Two Methods (continued) Get block: contains code to allow an application to retrieve the contents of the variable exposed as a Property Set block: allows an application to assign a value to the variable exposed as a Property ReadOnly keyword: makes a property readable but not settable (no Set block) WriteOnly keyword: makes a property settable but not readable (no Get block) Microsoft Visual Basic 2005: Reloaded, Second Edition 22

Constructors Constructor: A method whose instructions are processed automatically when an object is instantiated from a class Purpose is to initialize the class s Private variables Method name must be New May or may not have parameters Default constructor: a constructor with no parameters Microsoft Visual Basic 2005: Reloaded, Second Edition 23

Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 24

Methods Other than Constructors Methods in a class can be Sub or Function procedures Use Pascal casing for method names Microsoft Visual Basic 2005: Reloaded, Second Edition 25

Methods Other than Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 26

Methods Other than Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 27

Methods Other than Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 28

Methods Other than Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 29

Example 3 Using a Class that Contains Two Constructors Microsoft Visual Basic 2005: Reloaded, Second Edition 30

Using a Class that Contains Two Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 31

Using a Class that Contains Two Constructors (continued) Parameterized constructor: a constructor method that contains parameters Method signature: method name and parameter list Best practices: Parameterized constructor should set the values of the class variables using the class s Property procedures to take advantage of any validation code in the Property procedures Microsoft Visual Basic 2005: Reloaded, Second Edition 32

Using a Class that Contains Two Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 33

Using a Class that Contains Two Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 34

Using a Class that Contains Two Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 35

Using a Class that Contains Two Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 36

Using a Class that Contains Two Constructors (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 37

Example 4 Using a Class that Contains Overloaded Methods Microsoft Visual Basic 2005: Reloaded, Second Edition 38

Using a Class that Contains Overloaded Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 39

Using a Class that Contains Overloaded Methods (continued) Overloaded methods: two or more methods that have the same name but different parameters Constructors can be overloaded To overload a non-constructor method, use the Overloads keyword in the method declaration Many of VB s built-in methods are overloaded, as shown in the Intellisense feature: Microsoft Visual Basic 2005: Reloaded, Second Edition 40

Using a Class that Contains Overloaded Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 41

Using a Class that Contains Overloaded Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 42

Using a Class that Contains Overloaded Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 43

Using a Class that Contains Overloaded Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 44

Using a Class that Contains Overloaded Methods (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 45

Example 5 Using a Base Class and a Derived Class Inheritance: one class can be created from another class Base class: the original class Derived class: the new class created from the base class Inherits keyword: specifies the base class Microsoft Visual Basic 2005: Reloaded, Second Edition 46

Using a Base Class and a Derived Class (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 47

Using a Base Class and a Derived Class (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 48

Using a Base Class and a Derived Class (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 49

Using a Base Class and a Derived Class (continued) Derived class contains all of the attributes and behaviors of the base class Derived class may also contain its own attributes and behaviors Overridable keyword: indicates the base class method can be overridden by the derived class Overrides keyword: indicates that a method in the derived class overrides the method with the same name in the base class Microsoft Visual Basic 2005: Reloaded, Second Edition 50

Using a Base Class and a Derived Class (continued) MyBase keyword: refers to the base class MyBase.New: tells the computer to process the code in the base class s constructor Microsoft Visual Basic 2005: Reloaded, Second Edition 51

Using a Base Class and a Derived Class (continued) Microsoft Visual Basic 2005: Reloaded, Second Edition 52

Programming Tutorial Microsoft Visual Basic 2005: Reloaded, Second Edition 53

Programming Example Microsoft Visual Basic 2005: Reloaded, Second Edition 54

Summary Objects are instantiated from classes Classes encapsulate their attributes and behaviors Class Public data members and methods are exposed to any application that creates an object from the class; class Private members are not Class Public properties allow an application to manipulate private class data members ReadOnly keyword allows a property to be retrieved but not changed Microsoft Visual Basic 2005: Reloaded, Second Edition 55

Summary (continued) WriteOnly allows a property to be changed but not retrieved Property Get block implements retrieving the property value Property Set block implements changing the property value Constructor: a sub procedure named New that is processed when an object is created from the class Default constructor: has no parameters Microsoft Visual Basic 2005: Reloaded, Second Edition 56

Summary (continued) Derived class: a new class created from another class (the base class) Overridable keyword: indicates that a method in the base class can be overridden in the derived class Overrides keyword: indicates that a method in the derived class overrides a method of the same name in the base class Inherits clause: creates a derived class MyBase keyword: refers to the base class Microsoft Visual Basic 2005: Reloaded, Second Edition 57