12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.

Slides:



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

Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
More about classes and objects Classes in Visual Basic.NET.
OOP-Creating Object-Oriented Programs
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
OOP: Creating Object-Oriented Programs. VB & Object Oriented Programming Objects have properties, methods, and generate events Classes have been predefined.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Advanced Object-Oriented Programming Features
Creating Object Oriented Programs Object oriented (OO) terminology Class vs. object Instantiate an object in a project Understand Class_Initialize / Terminate.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
ASP.NET Programming with C# and SQL Server First Edition
Chapter 13: Object-Oriented Programming
VB Classes ISYS 512. Adding a Class to a Project Project/Add Class –*** MyClass is a VB keyword. Steps: –Adding properties Declare Public variables in.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Object-Oriented Programming in Visual Basic.NET. Overview Defining Classes Creating and Destroying Objects Inheritance Interfaces Working with Classes.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
To define a class in Visual Basic.NET, you can follow this general procedure: 1. Add a class to the project. 2. Provide an appropriate file name for.
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
A First Program Using C#
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 8 More Object Concepts
110-I 1 Object Terminology Review Object - like a noun, a thing –Buttons, Text Boxes, Labels Properties - like an adjective, characteristics of object.
Module 7: Object-Oriented Programming in Visual Basic .NET
An Object-Oriented Approach to Programming Logic and Design
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
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 8 Using Procedures and Exception Handling.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Chapter 9: Getting Comfortable with Object- Oriented Programming.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Programming in Java CSCI-2220 Object Oriented Programming.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
CSCI 3327 Visual Basic Chapter 9: Object-Oriented Programming: Classes and Objects UTPA – Fall 2011.
Object-Oriented Application Development Using VB.NET 1 Chapter 8 Understanding Inheritance and Interfaces.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
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.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Object-Oriented Programming: Classes and Objects.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance and Polymorphism
Microsoft Visual Basic 2005: Reloaded Second Edition
Using Procedures and Exception Handling
Understanding Inheritance
Chapter 11 – Object-Oriented Programming
VISUAL BASIC.
IS437: Fall 2004 Instructor: Dr. Boris Jukic
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance and Polymorphism
CIS16 Application Development and Programming using Visual Basic.net
Presentation transcript:

12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf

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

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

12-6 Objects VB allows the creation of new object types by creating a class Classes may have both properties and methods, and it can perform like an event too 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-7 "Cookie Analogy" Class = Cookie, 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-8 Object-Oriented Terminology Encapsulation Inheritance Polymorphism Reusable Classes Multitier Applications

12-9 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-10 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-11 Inheritance (2 of 2) Examine first line of code for a form in the FormName.Designer.vb file Public Class Form1 Inherits System.Windows.Forms.Form Inherited Class: Subclass, Derived Class, Child Class Original Class: Base Class, Superclass, Parent Class

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

12-13 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 callling the method Example: MessageBox.Show method Overriding -Refers to a class that has the same method name as its base class Method in subclass takes precedence

12-14 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-15 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-16 Three-tier Model Most popular implementation

12-17 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-18 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, that would violate encapsulation (each object should be in charge of its own data)

12-19 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-20 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-21 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-22 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 class-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-23 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 ' Private method used for internal calculations Private Sub CalculateExtendedPrice() ‘Calculate the extended price extendedPriceDecimal = quantityInteger * priceDecimal End Sub

12-24 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

12-25 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 Sub New() ' Empty constructor End Sub Sub New(ByVal Title As String, ByVal Quantity As Integer, _ ByVal Price As Decimal) ' Code statements to assign property values End Sub

12-26 Parameterized Constructor Constructor that requires arguments Allows arguments to be passed when creating an object ' Instantiate the object and set the properties aBookSale = New BookSale(Me.titleTextBox.Text, _ Integer.Parse(Me.quantityTextBox.Text), _ Decimal.Parse(Me.priceTextBox.Text)) Sub New(ByVal Title As String, ByVal Quantity As Integer, ByVal Price As Decimal) ' Code statements to assign property values With Me.Title = Title.Quantity = Quantity.Price = Price End With CalculateExtendedPrice() End Sub

12-27 Create 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-28 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 In VB it is ok to declare and instantiate an object at the same time Private aBookSale As BookSale aBookSale = New BookSale( ) Or Dim aBookSale As New Booksale( )

12-29 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-30 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-31 Shared Members in MSDN Help

12-32 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-33 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 Public Class NewClass Inherits BaseClass

12-34 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-35 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-36 Calling the Base Class Constructor Just like any other inherited method, the base class constructor can be called from the derived class Must be the first statement in the constructor for the inherited class Sub New(ByVal Title As String, ByVal Quantity As Integer, _ ByVal Price As Decimal) ‘ Call the base class constructor MyBase.New(Title, Quantity, Price) End Sub

12-37 Overriding Methods (1 of 2) 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-38 Overriding Methods (2 of 2) Use the MustOverride keyword in the base class to override abstract methods Abstract methods are empty methods (with no code) Any class that has any class declared as MustOverride is an abstract class Base Class Protected Overridable Sub SomeProcedure() or Protected MustOverride Sub SomeProcedure() Inherited Class Protected Overrides Sub SomeProcedure()

12-39 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 The properties of the base class that have both Get and Set accessor methods can be referred directly in methods in the derived class Read-only or write-only properties cannot be accessed by name from a derived class

12-40 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-41 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-42 Creating Inherited Form Class Project menu, Add Windows Form Modify the Inherits Statement to inherit from base form using project name as the namespace --OR-- Project menu, Add Inherited Form In dialog select name of base form

12-43 Base and Inherited Forms

12-44 Coding for Events of an Inherited Class Event handlers defined in the base class are inherited and available for use in the derived class Copy procedure from base class into derived class to make modifications Event handlers for new controls in the derived class can be created the usual way

12-45 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 add 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-46 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-47 The Object Browser Window

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

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

12-50 Examining Your Own Classes