CSCI 3327 Visual Basic Chapter 3: Classes and Objects UTPA – Fall 2011.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
OOP / VB.NET (Chp4) DR. JOHN ABRAHAM UTPA Credit: Partial slides from startvbdotnet.com.
Chapter 11: Implementing Inheritance and Association Visual Basic.NET Programming: From Problem Analysis to Program Design.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
12-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
 2008 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
VB Classes ISYS 573. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
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.
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.
 2009 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
CSCI 3327 Visual Basic Chapter 1: Introduction to Visual Basic
Introduction to Classes and Objects (Through Ch 5) Dr. John P. Abraham Professor UTPA.
Programming Languages and Paradigms Object-Oriented Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 8 More Object Concepts
Module 7: Object-Oriented Programming in Visual Basic .NET
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
CONCEPTS OF OBJECT ORIENTED PROGRAMMING. Topics To Be Discussed………………………. Objects Classes Data Abstraction and Encapsulation Inheritance Polymorphism.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
1.  A method describes the internal mechanisms that actually perform its tasks  A class is used to house (among other things) a method ◦ A class that.
Chapter 9: Getting Comfortable with Object- Oriented Programming.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Chapter 6 OOP: Creating Object-Oriented Programs Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
RIT Computer Science Dept. Goals l Inheritance l Modifiers: private, public, protected l Polymorphism.
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.
Chapter 4 Introduction to Classes, Objects, Methods and strings
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Object-Oriented Programming. Objectives Distinguish between object-oriented and procedure-oriented design. Define the terms class and object. Distinguish.
Introduction to Object-Oriented Programming Lesson 2.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Classes, Interfaces and Packages
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.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Object-Oriented Programming: Classes and Objects Chapter 1 1.
PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.
Object-Oriented Programming: Classes and Objects.
Object-Oriented Programming: Classes and Objects
Chapter 3: Using Methods, Classes, and Objects
Object-Oriented Programming: Classes and Objects
Lecture 22 Inheritance Richard Gesick.
Advanced Programming Behnam Hatami Fall 2017.
CSCI 3328 Object Oriented Programming in C# Chapter 3: Introduction to Classes and Objects UTPA – Fall 2012 This set of slides is revised from lecture.
Classes, Objects, Methods and Strings
Object-Oriented Programming: Classes and Objects
Tonga Institute of Higher Education
Fundaments of Game Design
Classes & Objects A deeper Look Chapter 10 & 11
Visual Programming Lecture 4.
Chapter 11 Inheritance and Encapsulation and Polymorphism
Presentation transcript:

CSCI 3327 Visual Basic Chapter 3: Classes and Objects UTPA – Fall 2011

Objectives In this chapter, you will –Become aware of reasons for using objects and classes –Become familiar with classes and objects 2

Introduction The book uses car analogy We humans are very good in recognizing and working with objects, such as a pen, a dog, or a human being. We learned to categorize them in such a way that make sense to us. We may categorize them as animate object, inanimate objects, pets, friends, etc. 3

Introduction (cont'd) We some times classify objects based on their attributes, for example, green apples or red apples, fat or slim people, etc. If you think about it each object has many attributes. If I ask you list the attributes of an orange, you probably could list many things such as color, shape, weight, smell, etc. 4

Introduction (cont'd) In addition to attributes, all objects exhibit behaviors A dog eats, barks, wags its tail, plays, and begs. A dog exhibits many more other behaviors than this short list Another thing we need to remember about objects is that objects interact between each other 5

Objects Objects are packages that contain data and functions (methods) that can be performed on the data. 6

Objects (cont'd) Data could be considered to be attributes and functions are considered to be behaviors of the object. We can say that the attributes and behaviors are encapsulated into an object. 7

Objects (cont'd) The objects interact between each other through their interfaces. As an example a date object may have a set of data consisting of month, day and year, and methods consisting of assign date, display date, yesterday and tomorrow. 8

Classes and Objects in Visual Basic Classes are types and Objects are instances of the Class. Without objects you cannot use a class –Your book says you cannot drive the drawing of a car In Visual Basic, we create a class with the Class statement and end it with End Class 9

A Class Looks Like This: Public Class House Public Rooms As Integer End Class 10

Members of a Class Fields, Properties, Methods and Events They can be declared as Public, Private, Protected, Friend or Protected Friend Fields and Properties represent information that an object contains Fields of a class are like variables and they can be read or set directly –For example, if you have an object named House, you can store the numbers of rooms in it in a field named Rooms 11

The Syntax for a Class Public Class Test '-----Variables '-----Methods '-----Properties '-----Events End Class 12

Instance Variables (Attributes) Examples: Amount of gas in your car Balance in your bank account 13

Using Objects Object: car Pressing the gas pedal sends a message to the car to perform a task Similarly, in programming, you use a method call to send a message to the object 14

Methods (Behavior) Methods represent the object’s built-in procedures. For example, a Class named Country may have methods named Area and Population. 15

Defining Methods You define methods by adding procedures, Sub routines or functions to your class For example, implementation of the Area and Population methods discussed above might look like next slide 16

Example of Defining Methods Public Class Country Public Sub Area() Write(" ") End Sub Public Sub population() Write(" ") End Sub End Class 17

Properties (Attributes) Properties are retrieved and set like fields If they are public, they can be changed by clients Private properties are implemented using Property Get and Property Set procedures which provide more control on how values are set or returned 18

Properties (cont'd) Get and set appear to be similar to declaring the properties public Through get and set, programmer can have control over setting the properties –For instance, if a client tries to set a date to 37, it can be checked and set to a default value instead 19

Example GradeBook Public Class GradeBook Private courseNameValue As String = "Not set yet" Public Property CourseName() As String Get Return courseNameValue End Get Set(ByVal value As String) courseNameValue = value End Set End Property Public Sub displayMessage() Console.WriteLine("Welcome to the grade book for " & vbNewLine & courseName & "!") End Sub End Class 20

Example Module Module GradeBookTest Sub Main() Dim theName As String Dim gradebook As New GradeBook Console.WriteLine("Initial course name is: " & gradebook.CourseName & vbNewLine) Console.WriteLine("Please enter course name: ") theName = Console.ReadLine() gradebook.CourseName = theName Console.WriteLine() gradebook.displayMessage() Console.ReadKey() End Sub End Module 21

Events We have been programming for events already Events allow objects to perform actions whenever a specific occurrence takes place –For example, when we click a button a click event occurs and we can handle that event in an event handler 22

Creating An Object After defining a class, instance of the class, object can be created To create a object for this class (Test) we use the new keyword and that looks like this: Dim obj As New Test() 23

Sample Program Imports System.Console Module Module1 Sub Main() Dim obj As New Test obj.disp() Read() End Sub End Module Public Class Test Sub disp() 'a method named disp in the class Write("Welcome to OOP") End Sub End Class 24

Classes Constructor Destructor Properties Methods Inheritance Polymorphism 25

Constructor A constructor is a special member function whose task is to initialize the objects of its class A constructor is invoked whenever an object of its associated class is created 26

Constructor (cont'd) If a class contains a constructor, then an object created by that class will be initialized automatically We pass data to the constructor by enclosing it in the parentheses following the class name when creating an object In Visual Basic we create constructors by adding a Sub procedure name New to a class 27

Initializing Objects with Constructors in Visual Basic The constructor class is called by the class name followed by parenthesis. Constructor subroutine can be declared with the "New" keyword. –Public Sub New (ByVal initialBalance As Decimal) Set default value in the constructor –Public Sub New (Optional ByVal initialBalance As Decimal = 0D) 28

Imports System.Console Module Module2 Sub Main() Dim startObject As New Constructor (10) 'storing a value in the constructor by passing a value(10) WriteLine(startObject.display()) 'calling it with the display method Read() 'reading from keyboard End Sub End Module Public Class Constructor Public x As Integer Public Sub New (ByVal value As Integer) 'constructor x = value 'storing the value of x in constructor End Sub Public Function display() As Integer Return x 'returning the stored value End Function End Class 29

Destructor Destructors run when an object is destroyed Within a destructor we can place code to clean up the object after it is used We use "Finalize" method in Visual Basic –The Finalize method is called automatically when the.NET runtime determines that the object is no longer required 30

Destructor (cont'd) When working with destructors we need to use the overrides keyword with Finalize method as we will override the Finalize method built into the Object class We normally use Finalize method to deallocate resources and inform other objects that the current object is going to be destroyed The following code demonstrates the use of Finalize method 31

Imports System.Console Module Module3 Sub Main() Dim obj As New Destructor End Sub End Module Public Class Destructor Protected Overrides Sub Finalize() Write(“Destroyed!") Read() End Sub End Class 32

Property Values Get and Set Get allows you read the current property value Set allows you to change the property value Property without a "ReadOnly" or "WriteOnly" specifier must provide both a "Get" and a "Set" 33

Modules, Classes and Methods Both classes and modules contain methods (Subs) Related classes are grouped into namespaces and compiled into library files A module contains declarations and procedures that are used by other files in a project –Module is not associated with a class or form and contain no event procedures –Good to use in case you are using multiple forms 34

Private and Public in a Module Private – procedures and variables declared as private can only be accessed by statements in the same module Public – accessible by statements either inside or outside the module By default, Sub declarations are Public Variables declared with Dim or Private – variables are private 35

Inheritance A key feature of OOP is reusability It's always time saving It can be used by other programs to suit the program's requirement This is done by creating a new class from an existing class The process of deriving a new class from an existing class is called Inheritance 36

Inheritance (cont'd) The old class is called the base class The new class is called derived class The derived class inherits some or everything of the base class In Visual Basic we use the Inherits keyword to inherit one class from another 37

Inheritance (cont'd) New class is created from an existing one by adding new or modified capabilities The new class is a derived class or subclass –A derived class can become a base class for a future derived class Is-a relationship – represents inheritance –A car is a vehicle Has-a relationship – represents a composition –A car has a steering wheel 38

Inheritance Coding Public Class One End Class Public Class Two Inherits One End Class 39

Inheritance Coding (cont'd) Imports System.Console Module Module4 Sub Main() Dim ss As New Two WriteLine(ss.sum()) Read() End Sub End Module (see next slide for inheritance) 40

Public Class One Public i As Integer = 10 Public j As Integer = 20 Public Function add() As Integer Return i + j End Function End Class Public Class Two Inherits One Public k As Integer = 100 Public Function sum() As Integer 'using the variables, function from base class and adding more functionality Return i + j + k 'Return add() +k End Function End Class 41

Protected Members Public and Private In the base class, private members are not inherited by derived classes Protected access offers an intermediate level of access between Public and Private. –A base class’s protected members can be accessed only by members of that base class and by members of its derived classes 42

Friend Members Friend access is another intermediate level access If a program uses multiple classes from the same assembly, these classes can access each other’s friend members Unlike public access, programs that are declared outside the assembly cannot access these Friend Members 43

Example of Friend Members in Visual Basic 44

Polymorphism It means "one name, multiple forms" 45

Example of Polymorphism Suppose there is a base class called Animal with a method called move There are three derived classes called fish, frog and bird Program calls move to all three objects of the derived class –Fish swims, frog jumps, and birds fly –This is possible because of polymorphism 46

Polymorphism Analogy If you are asked to open, you will know what to do depending on what the object you are working with: a door, a jar, an envelope, etc. 47

Abstract and Concrete Classes Abstract Classes that are not used to make objects Abstract classes are incomplete and are used only as base classes to make derived classes Derived classes that can be used to instantiate objects are called concrete classes 48