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

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

Advanced Programming Using Visual Basic 2008

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

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

Building Multitier Programs with Classes

CIS Intro to Programming in C#

Microsoft Visual Basic 2005: Reloaded

Chapter 02 Building Multitier Programs with Classes

Object-Oriented Programming

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

Computer Science 4U Unit 1. Programming Concepts and Skills Modular Design

Lecture 10 OOP and VB.Net

VB.NET. Exercise 1: Creating Your First Application in Visual Basic.NET

Public Class ClassName (naming: CPoint)

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

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

Chapter 5 Object-Oriented Programming

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS260 Intro to Java & Android 03.Java Language Basics

PROGRAMMING LANGUAGE 2

Overview of OOP. Dr. Zhang COSC 1436 Summer, /18/2017

Understanding Inheritance and Interfaces

CS313D: ADVANCED PROGRAMMING LANGUAGE

Programming II (CS300)

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

CMSC 132: Object-Oriented Programming II

CS313D: ADVANCED PROGRAMMING LANGUAGE

Inheritance, and Polymorphism.

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

Microsoft Visual Basic 2005: Reloaded

M Introduction to Visual Basic.NET Programming with Microsoft.NET 5 Day Course

DEVELOPING OBJECT ORIENTED APPLICATIONS

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Programming II (CS300)

Lecture Contents CS313D: ADVANCED PROGRAMMING LANGUAGE

Object Oriented Programming in Java. Jaanus Pöial, PhD Tallinn, Estonia

Software Development. Modular Design and Algorithm Analysis

Get Unique study materials from

JAVA MOCK TEST JAVA MOCK TEST II

Data Abstraction. Hwansoo Han

2. A GUI A. uses buttons, menus, and icons B. should be easy for a user to manipulate C. both (a) and (b) D. stands for Graphic Use Interaction

OVERRIDING. 7/11/2015 Budditha Hettige 82

VB.NET Web : Phone : INTRODUCTION TO NET FRAME WORK

Polymorphism 2/12/2018. Which statement is correct about overriding private methods in the super class?

Programming Language Concepts Object-Oriented Programming. Janyl Jumadinova 28 February, 2017

Object Orientated Analysis and Design. Benjamin Kenwright

CREATED BY: Muhammad Bilal Arslan Ahmad Shaad. JAVA Chapter No 5. Instructor: Muhammad Naveed

What are the characteristics of Object Oriented programming language?

Inheritance. Lecture 11 COP 3252 Summer May 25, 2017

Module 10 Inheritance, Virtual Functions, and Polymorphism

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

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

2559 : Introduction to Visual Basic.NET Programming with Microsoft.NET

Chapter 6. Multiform Projects The McGraw-Hill Companies, Inc. All rights reserved. McGraw-Hill

Advanced Object-Oriented Programming. 11 Features. C# Programming: From Problem Analysis to Program Design. 4th Edition

CLASSES AND OBJECTS IN JAVA

What is Inheritance?

Java Object Oriented Design. CSC207 Fall 2014

Programming Exercise 14: Inheritance and Polymorphism

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

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

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

VALLIAMMAI ENGINEERING COLLEGE

INHERITANCE. Spring 2019

G Programming Languages - Fall 2012

Inheritance and Polymorphism

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

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

Data Structures using OOP C++ Lecture 3

CS313D: ADVANCED PROGRAMMING LANGUAGE

QUESTIONS FOR AVERAGE BLOOMERS

Lecture 23: Object Lifetime and Garbage Collection

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

Inheritance and Encapsulation. Amit Gupta

VB.NET MOCK TEST VB.NET MOCK TEST I

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Inheritance and Substitution (Budd chapter 8, 10)

IT101. Inheritance, Encapsulation, Polymorphism and Constructors

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

Chapter 11. Categories of languages that support OOP: 1. OOP support is added to an existing language

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

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

Compaq Interview Questions And Answers

M. K. Institute Of Computer Studies, Bharuch SYBCA SEM IV VB.NET (Question Bank)

Object Oriented Programming. Assistant Lecture Omar Al Khayat 2 nd Year

Chapter 11 Object and Object- Relational Databases

Cpt S 122 Data Structures. Introduction to C++ Part II

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

Java. Classes 3/3/2014. Summary: Chapters 1 to 10. Java (2)

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

Chapter 5. Inheritance

Introduction to Object- Oriented Programming


Inheritance -- Introduction

Practice for Chapter 11

CS11 Introduction to C++ Fall Lecture 7

CMSC 132: Object-Oriented Programming II

AN OVERVIEW OF C++ 1

C++ Inheritance and Encapsulation

Transcription:

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

Objectives (1 of 2) Use object-oriented terminology correctly. Create a two-tier application that separates the user interface from the business logic. Differentiate between a class and an object. Create a class that has properties and methods. Declare object variables and use property procedures to set and retrieve properties of a class. 12-2

Objectives (2 of 2) Assign values to the properties with a constructor. Instantiate an object in a project using your class. Differentiate between shared members and instance members. Understand the purpose of the constructor and destructor methods. Inherit a new class from your own class. Use visual inheritance by deriving a form from another form. 12-3

Object-Oriented Programming (OOP) OOP is currently the most accepted style of programming. Java, C# and SmallTalk were designed to be OO (object oriented) from inception. VB and C++ have been modified to accommodate OOP. VB.NET 2003 was the first version of VB to be truly object oriented. As projects become more complex, using objects becomes increasingly important. 12-4

Objects VB allows the creation of new object types by creating a class. Classes may have properties, methods, and events. Objects are things such as buttons; buttons is a class, but exitbutton is an instance of the class the instance is the object. There may be many objects of a new class type. 12-5

"Cookie Analogy" Class = Cookie cutter Instantiate = Making a cookie using the cookie cutter Instance = Newly made cookie Properties of the Instance may have different values. Icing property can be True or False. Flavor property could be Lemon or Chocolate Methods = Eat, Bake, or Crumble Events = Cookie crumbling and informing you 12-6

Object-Oriented Terminology Encapsulation Inheritance Polymorphism Reusable Classes Multitier Applications 12-7

Encapsulation Combination of characteristics of an object along with its behavior in "one package" Cannot make object do anything it does not already "know" how to do. Cannot make up new properties, methods, or events. Sometimes referred to as data hiding, an object can expose only those data elements and procedures that it wishes. 12-8

Inheritance (1 of 2) Ability to create a new class from an existing class Original class is called Base Class, Superclass, or Parent Class. Inherited class is called Subclass, Derived Class, or Child Class. For example, each form created is inherited from the existing Form class. Purpose of inheritance is reusability. 12-9

Inheritance (2 of 2) Examine first line of code for a form in the Editor. Inherited Class: Subclass, Derived Class, Child Class Partial Public Class Form1 Inherits System.Windows.Forms.Form Original Class: Base Class, Superclass, Parent Class 12-10

Base Class Person Subclasses Employee Customer Student The derived classes inherit from the base class. Inheritance Example 12-11

Polymorphism Methods having identical names, but different implementations Radio button, check boxes, and list boxes all have a Select method the Select method operates appropriately for its class. Overloading Several argument lists for calling the method Example: MessageBox.Show method Overriding Refers to a method that has the same name as its base class Method in subclass takes precedence. 12-12

Reusability Big advantage of OOP over traditional programming New classes created can be used in multiple projects. Each object created from the class can have its own properties. 12-13

Multitier Applications Classes are used to create multitier applications. Each of the functions of a multitier application can be coded in a separate component and stored and run on different machines. Goal is to create components that can be combined and replaced. 12-14

Three-tier Model Most popular implementation 12-15

Designing Your Own Class Analyze characteristics needed by new objects. Characteristics or properties are defined as variables. Define the properties as variables in the class module. Analyze behaviors needed by new objects. Behaviors are methods. Define the methods as sub procedures or functions. 12-16

Creating Properties in a Class Define variables inside the Class module by declaring them as Private these store the value of the properties of the class. Do not make Public, since that would violate encapsulation (each object should be in charge of its own data). 12-17

Property Procedures Properties in a class are accessed with accessor methods in a property procedure. Name used for property procedure is the name of the property seen by the outside world. Set accessor method. Uses Value keyword to refer to incoming value for property Assigns a value to the property Get Statement uses the value keyword to refer to the incoming value of the property. Must assign a return value to the procedure name or use a Return Statement to return a value. 12-18

Property Procedure General Form {Private Protected } ClassVariable As DataType [Public] Property PropertyName( ) As DataType Get Return ClassVariable [ PropertyName = ClassVariable] End Get Set (ByVal Value As DataType) [statements, such As validation] ClassVariable = Value End Set End Property 12-19

Read-Only Properties In some instances, a value for a property can be retrieved by an object but not changed. A property can be written to create a read-only property. Create a read-only property by using the ReadOnly modifier. ' The property procedure for a read-only property. ReadOnly Property TotalPay() As Decimal Get Return TotalPayDecimal End Get End Property 12-20

Write-Only Properties At times, a property can be assigned by an object, but not retrieved. Create a property block that contains only a Set to create a write-only property. ' Private module-level variable to hold the property value. Private PriceDecimal As Decimal Public WriteOnly Property Price() As Decimal Set(ByVal value As Decimal) If value >= 0 Then PriceDecimal = value End If End Set End Property 12-21

Class Methods Create methods by coding public procedures within a class. Methods declared with the Private keyword are available only within the class. Methods declared with the Public keyword are available to external objects. 12-22

Constructors and Destructors Constructor Method that automatically executes when an object is instantiated Constructor must be public and is named New. Ideal location for an initialization tasks such as setting the initial values of variable and properties Destructor Method that automatically executes when an object is destroyed Rarely needed in.net classes (automatic garbage collection) 12-23

Overloading the Constructor Overloading means that two methods have the same name but a different list of arguments (the signature). Create by giving the same name to multiple procedures in a class module, each with a different argument list. 12-24

Parameterized Constructor Constructor that requires arguments Allows arguments to be passed when creating an object 12-25

Creating a New Class Steps Project, Add Class. In Add New Item dialog box, choose Class. Name the Class. Define the Class properties. To allow access from outside the class, add property procedures. Code the methods. 12-26

Creating a New Object Using a Class (1 of 2) Similar to creating a new tool for the toolbox but not yet creating an instance of the class Two-step operations Declare a variable for the new object. Instantiate the object using the New keyword. Private TheBookSale As BookSale TheBookSale = New BookSale( ) Or Dim TheBookSale As New Booksale( ) In VB, it is ok to declare and instantiate an object at the same time. 12-27

Creating a New Object Using a Class ( 2 of 2) If object variable is needed in multiple procedures, declare the object at class level. Instantiate the object. May need to include a New statement and a Try/Catch block for error handling (Try/Catch block must be inside a procedure.) The preferred technique is to include the New statement inside of a procedure at the time the object is needed. Pass values for the arguments at instantiation when using a parameterized constructor. 12-28

Instance Variables versus Shared Variables Instance variables or properties Separate memory location for each instance of the object Shared variables or properties Single variable that is available for ALL objects of a class Can be accessed without instantiating an object of the class When creating, use the Shared keyword to create. Shared properties can be set to read-only so that their values can be retrieved but not set directly. 12-29

Shared Members in MSDN Help 12-30

.NET Automatic Garbage Collection Feature of.net Common Language Runtime (CLR) that cleans up unused components Periodically checks for unreferenced objects and releases all memory and system resources used by the objects Microsoft recommends depending on garbage collection to release resources rather than Finalize procedures. 12-31

Inheritance 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 Use the Inherits statement following the class header and prior to any comments 12-32

Inheriting Properties and Methods Public and protected data members and methods of the base class are inherited in the derived class. Must write the method in the derived class if wanting to override the base-class method. Can declare elements with the Protected keyword which specifies that the element is accessible only within its own class or any class derived from that class. 12-33

Constructors in Inheritance There is one exception for a derived class inheriting all public and protected methods: A subclass cannot inherit constructors from the base class. Each class must have its own constructors. Exception is if the only constructor needed is an empty constructor VB automatically creates an empty constructor for all classes. 12-34

Overriding Methods Methods with the same name and the same argument list as the base class Derived class (subclass) will use the new method rather than the method in the base class. To override a method Declare the original method with the Overridable keyword. Declare the new method with the Overrides keyword. The access modifier for the base-class procedure can be Private or Protected (not Public). 12-35

Accessing Properties Derived class can set and retrieve base class properties by using the property accessor methods. Call the base class constructor from the derived class constructor to use the property values from the base class. If the constructor requires arguments, the values can be passed when the constructor is called. 12-36

Creating a Base Class Strictly for Inheritance Classes can be created strictly for inheritance by two or more similar classes, and are never instantiated. For a base class that you intend to inherit from, include the MustInherit modifier on the class declaration. In each base class method that must be overridden, include the MustOverride modifier and no code in the base class method. 12-37

Inheriting Form Classes Many projects require several forms. Create a base form and inherit the visual interface to new forms. Base form inherits from System.Windows.Forms.Form. New form inherits from Base form. 12-38

Creating Inherited Form Class In the Project menu, Add Windows Form. Modify the Inherits Statement to inherit from base form using project name as the namespace. --OR-- In the Project menu, Add Inherited Form. In dialog, select name of base form. 12-39

Base and Inherited Forms 12-40

Coding for Events of an Inherited Class Copy procedure from base class into derived class and make modifications. An alternate way is to use the inherited event handler in the derived class. 12-41

Managing Multiclass Projects VB projects are automatically assigned a namespace which defaults to the name of the project. Add an existing class to a project by copying the file into the project folder and then adding the file to the project using Project/Add Existing Item. --OR-- Right-click the project name in the Solution Explorer and select Add/Existing Item from the context menu. 12-42

Using the Object Browser Use it to view the names, properties, methods, events, and constants of VB objects, your own objects, and objects available from other applications. To Display Select View/Object Browser OR-- Object Browser toolbar button. 12-43

The Object Browser Window 12-44

Examining VB Classes (1 of 2) Members of System.Windows.Forms.MessageBox Class 12-45

Examining VB Classes (2 of 2) Display the MessageBoxButtons Constants 12-46

Examining Your Own Classes 12-47