Chapter 11 – Object-Oriented Programming

Slides:



Advertisements
Similar presentations
SQL SELECT fields FROM table WHERE search condition GROUP BY grouping_columns HAVING search_condition ORDER BY sort_fields.
Advertisements

CS 211 Inheritance AAA.
Inheritance Inheritance Reserved word protected Reserved word super
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
More about classes and objects Classes in Visual Basic.NET.
OOP-Creating Object-Oriented Programs
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Aalborg Media Lab 23-Jun-15 Inheritance Lecture 10 Chapter 8.
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)
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Object Based Programming. Summary Slide  Instantiating An Object  Encapsulation  Inheritance  Polymorphism –Overriding Methods –Overloading vs. Overriding.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
Module 7: Object-Oriented Programming in Visual Basic .NET
An Object-Oriented Approach to Programming Logic and Design
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Classes and Objects noun A word used to denote or name a person, place, thing, quality, or act. verb That part of speech that expresses existence,
Chapter 11 - VB 2005 by Schneider1 Chapter 11 – Object-Oriented Programming 11.1 Classes and Objects 11.2 Arrays of Objects; Events; Containment 11.3 Inheritance.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
Advanced Object- Oriented Programming Programming Right from the Start with Visual Basic.NET 1/e 14.
CSCI 3327 Visual Basic Chapter 3: Classes and Objects UTPA – Fall 2011.
Chapter 6 OOP: Creating Object-Oriented Programs Programming In Visual Basic.NET.
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
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 © 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.
© 2007 Lawrenceville Press Slide 1 Chapter 9 Inheritance  One class is an extension of another.  Allows a class to define a specialized type of an existing.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
IT293 Intermediate Visual Basic Programming Unit 8: Object-oriented I Sydney Liles AIM: sydneyliles.
Chapter 13 Copyright 2000 All rights reserved 1 Chapter 13 Object-Oriented Programming.
Object-Oriented Programming: Inheritance and Polymorphism.
Chapter 2 11/18/2015 © by Pearson Education, Inc. All Rights Reserved. Lect9 GC 2011.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
Object-Oriented Programming Classes and Objects 11.2 Working with Objects 11.3 Inheritance.
Object-Oriented Programming: Classes and Objects.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
These materials where developed by Martin Schray
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Chapter 11 – Object-Oriented Programming
Object-Oriented Programming: Classes and Objects
Final and Abstract Classes
One class is an extension of another.
Microsoft Visual Basic 2005: Reloaded Second Edition
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance
Object Oriented Analysis and Design
One class is an extension of another.
Object-Oriented Programming: Inheritance
Object Based Programming
Corresponds with Chapter 7
Lecture 22 Inheritance Richard Gesick.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Classes and Objects
Object-Oriented Programming: Inheritance and Polymorphism
Object-Oriented Programming: Inheritance
Object-Oriented Programming
CIS16 Application Development and Programming using Visual Basic.net
Final and Abstract Classes
Object-Oriented Programming: Inheritance
C++ Object Oriented 1.
Presentation transcript:

Chapter 11 – Object-Oriented Programming 11.1 Classes and Objects 11.2 Arrays of Objects; Events; Containment 11.3 Inheritance Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider 11.1 Classes and Objects noun A word used to denote or name a person, place, thing, quality, or act. verb That part of speech that expresses existence, action, or occurrence. adjective Any of a class of words used to modify a noun or other substantive by limiting, qualifying, or specifying. The American Heritage Dictionary of the English Language Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider OOP Analogy Classes are like nouns Methods are like verbs Properties are like adjectives Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider OOP Terminology An object is an encapsulation of data and procedures that act on that data. "data hiding" prevents inadvertent data modification Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Objects in VB.NET Control objects – text boxes, list boxes, buttons, etc. Code objects – a specific instance of a user defined type called a class Class ClassName statements End Class Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Instances To create an instance of a control object, double-click on that control in the tool box. The control in the tool box is a template or blueprint of that control. You cannot set properties or invoke methods until you create an instance. Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Code Objects The user defined type represents the template or blueprint for the code object. This user defined type is called a class. Chapter 11 - VB.Net by Schneider

Instantiating a Code Object An object of a class can be declared with the statements: Dim objectName As className objectName = New className(arg1, arg2, ...) The Dim statement sets up a reference to the new object. The object is actually created with the word New. Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Common Tasks Assuming the following declaration: Dim objectName As New className(arg1, arg2, ...) Common Tasks: Task Statement Assign a value to a property objectName.propertyName = value Assign the value of a property to a variable varName = objectName.propertyName Carry out a method objectName.methodName(arg1, ...) Raise an event RaiseEvent eventName Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Private Data The following declaration may be inside a class: Private m_name As String 'Name The word "Private" is used to ensure that the variable cannot be accessed directly from outside the class. m_name is called an instance or member variable. Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Get and Set Public Property Name() As String Get Return m_name End Get Set(ByVal Value As String) m_name = Value End Set End Property Property block Must be inside the class to allow access to private data Additional code can be added to perform data validation before storing data in the member variable The word "public" allows for this to be accessed from outside the class Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Public vs. Private Items declared with the keyword Private cannot be accessed from outside the class. Those declared as Public are accessible from both inside and outside the class. Chapter 11 - VB.Net by Schneider

WriteOnly Property Procedures A property can be specified as "write only": Public WriteOnly Property MidGrade() As Double Set(ByVal Value As Double) m_midterm = Value End Set End Property Chapter 11 - VB.Net by Schneider

ReadOnly Property Procedures A property can be specified as "read only": Public ReadOnly Property MidGrade() As Double Get Return m_midTerm End Get End Property Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Methods Constructed with Sub and Functions Function CalcSemGrade() As String Dim grade As Double grade = (m_midterm + m_final) / 2 grade = Math.Round(grade) Select Case grade Case Is >= 90 Return "A" Case Is >= 80 Return "B" : End Function Chapter 11 - VB.Net by Schneider

Calls the Set property procedure Calls the Get property procedure Set and Get Examples Dim pupil As Student pupil = New Student() pupil.Name = "Adams, Al" txtBox.Text = pupil.Name lstBox.Items.Add(pupil.CalcSemGrade()) Calls the Set property procedure Calls the Get property procedure Calls the CalcSemGrade Method Chapter 11 - VB.Net by Schneider

Steps Used to Create a Class Identify a thing in your program that is to become an object. Determine the properties and methods that you would like the object to have. (As a rule of thumb, properties should access data, and methods should perform operations.) A class will serve as a template for the object. The code for the class is placed in a class block of the form Class ClassName statements End Class Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Steps continued For each of the properties in Step 2, declare a private member variable with a statement of the form Private m_variableName As DataType For each of the member variables in Step 4, create a Property block with Get and/or Set procedures to retrieve and assign values of the variable. For each method in Step 2, create a Sub procedure or Function procedure to carry out the task. Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Object Constructors Each class has a special method called a constructor that is always invoked when the object is instantiated. The constructor may take arguments. It is used to perform tasks to initialize the object. The first line of the constructor has the form: Public Sub New(ByVal par1 As dataType, ...) Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Example 3 Class Circle Private m_x As Integer Private m_y As Integer Private m_d As Integer Public Sub New() Xcoord = 0 Ycoord = 0 diameter = 40 End Sub Chapter 11 - VB.Net by Schneider

11.2 Arrays of Objects; Events; Containment "An object without an event is like a telephone without a ringer." -Anonymous Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Arrays of Objects Arrays have a data type That data type can be of User Defined Type Therefore, you can have arrays of objects Chapter 11 - VB.Net by Schneider

Using an Array of Objects Dim students(50) As Student Dim pupil As New Student() pupil.Name = txtName.Text pupil.SocSecNum = txtSSN.Text pupil.MidGrade = CDbl(txtMidterm.Text) pupil.Final = CDbl(txtFinal.Text) 'Add the student to the array lastStudentAdded += 1 students(lastStudentAdded) = pupil Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Events Can be created for classes User-Defined Events The statement for triggering an event is located in the class block The event is dealt with in the form’s code Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider User Defined Event Suppose that the event is named UserDefinedEvent and has the parameters par1, par2, and so on. In the class block, place the following statement in the Declarations section Public Event UserDefinedEvent(ByVal par1 As _ dataType1, ByVal par2 As dataType2, ...) The next statement should be placed at the locations in the class block code at which the event should be triggered RaiseEvent UserDefinedEvent(arg1, arg2, ...) Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Responding to Events When creating an object, the keyword WithEvents must be added so it will respond to events: Private WithEvents object1 As ClassName The declaration line of an event procedure would be Private Sub object1_UserDefinedEvent(par1, par2, ...) _ Handles object1.UserDefinedEvent Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Containment Class A contains class B when a member variable of class A is an object of type class B. Class DeckOfCards Private m_deck(52) As Card 'Class DeckOfCards contains class Card Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider 11.3 Inheritance The three relationships between classes are “use,” “containment,” and “inheritance.” One class uses another class if it manipulates objects of that class. Class A contains class B when a member variable of class A makes use of an object of type class B. Chapter 11 - VB.Net by Schneider

Inheritance relationship Inheritance is a process by which one class (the child or derived class) inherits the properties, methods, and events of another class (the parent or base class). The child has access to all of its parent’s properties, methods and events as well as to all of its own. If the parent is itself a child, then it and its children have access to all of its parent’s properties, methods and events. Chapter 11 - VB.Net by Schneider

Inheritance Hierarchy Consider the classes shown in Figure 11.6. All three children inherit Property A and Sub B from their parent. Child2 and Child3 also have an event and a property, respectively. GrandChild1 has access to Property A, Sub B, and Event C from its parent and adds Function E and Sub F. The collection of a parent class along with its descendants is called a hierarchy. Chapter 11 - VB.Net by Schneider

Benefits of Inheritance Allows two or more classes to share some common features yet differentiate themselves on others. Supports code reusability by avoiding the extra effort required to maintain duplicate code in multiple classes. Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Designing Objects Programmers need the ability to identify useful hierarchies of classes and derived classes. Software engineers are still working on the guidelines for when and how to establish hierarchies. The ISA test: If one class is a more specific case of another class, the first class should be derived from the second class. Chapter 11 - VB.Net by Schneider

Indentifies the Parent Inherits Class Parent Property A 'Property Get and Set blocks End Property Sub B() 'Code for Sub procedure B End Sub End Class Class Child2 Inherits Parent Event C() Indentifies the Parent Class: Child2 inherits From Parent Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Child class as parent Class GrandChild1 Inherits Child2 Function E() 'Code for function E End Sub Sub F() 'Code for Sub procedure F End Class Chapter 11 - VB.Net by Schneider

Polymorphism and overriding The set of properties, methods, and events for a class is called the class interface. The interface defines how the class will behave. Programmers only need to know how to use the interface in order to use the class. Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Polymorphism Literally means "many forms." The feature that two classes can have methods that are named the same and have essentially the same purpose, but different implementations, is called polymorphism. Chapter 11 - VB.Net by Schneider

Employing polymorphism A programmer may employ polymorphism in three easy steps. First, the properties, methods, and events that make up an interface are defined. Second, a parent class is created that performs the functionality dictated by the interface. Finally, a child class inherits the parent and overrides the methods that require different implementation than the parent. Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Overridable The keyword Overridable is used to designate the parent’s methods that are overridden, and the keyword Overrides is used to designate the child’s methods that are doing the overriding. There are situations where a child class's needs to access the parent class’s implementation of a method that the child is overriding. VB.NET provides the keyword MyBase to support this functionality. Chapter 11 - VB.Net by Schneider

Chapter 11 - VB.Net by Schneider Example Class PFStudent Inherits Student Overrides Function CalcSemGrade() As String 'The student's grade for the semester If MyBase.SemGrade = "F" Then Return "Fail" Else Return "Pass" End If End Function End Class Chapter 11 - VB.Net by Schneider

Abstract Properties, Methods and Classes Sometimes you want to insist that each child of a class have a certain property or method that it must implement for its own use. Such a property or method is said to be abstract and is declared with the keyword MustOverride. An abstract property or method consists of just a declaration statement with no code following it. It has no corresponding End Property, End Sub, or End Function statement. Its class is called an abstract base class and must be declared with the keyword MustInherit. Abstract classes cannot be instantiated, only their children can be instantiated. Chapter 11 - VB.Net by Schneider