What is an object?. What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex.

Slides:



Advertisements
Similar presentations
Python Objects and Classes
Advertisements

CS0007: Introduction to Computer Programming Introduction to Classes and Objects.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
ECE122 L4: Creating Objects February 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 4 Creating and Using Objects.
Road Map Introduction to object oriented programming. Classes
VBA Modules, Functions, Variables, and Constants
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
C++ fundamentals.
Lesson15. JavaScript Objects Objects encapsulate data (properties) and behavior(methods); the properties and the methods of an object are tied together.
Review of C++ Programming Part II Sheng-Fang Huang.
Using Data Active Server Pages Objectives In this chapter, you will: Learn about variables and constants Explore application and session variables Learn.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
1 Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Using Classes Object-Oriented Programming Using C++ Second Edition 5.
Writing Classes (Chapter 4)
 Java Programming Environment  Creating Simple Java Application  Lexical Issues  Java Class Library.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 6 Object-Oriented Java Script JavaScript, Third Edition.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
1 CS161 Introduction to Computer Science Topic #13.
Object Based Programming Chapter 8. 2 In This Chapter We will learn about classes Garbage Collection Data Abstraction and encapsulation.
Working With Objects Tonga Institute of Higher Education.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Indian Institute of Technology Bombay 1 OBJECTS Lecture #8 Manish Sinha.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
1 Programming Paradigms Object Orientated Programming Paradigm (OOP)
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
1 Chapter Four Creating and Using Classes. 2 Objectives Learn about class concepts How to create a class from which objects can be instantiated Learn.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
OOP (Object Oriented Programming) Lecture 1. Why a new paradigm is needed? Complexity Five attributes of complex systems –Frequently, complexity takes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Introduction to Object-Oriented Programming Lesson 2.
SEEM Java – Basic Introduction, Classes and Objects.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 24, 2009.
© 2004 Pearson Addison-Wesley. All rights reserved January 23, 2006 Creating Objects & String Class ComS 207: Programming I (in Java) Iowa State University,
1 n Object Oriented Programming. 2 Introduction n procedure-oriented programming consists of writing a list of instructions and organizing these instructions.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
© 2004 Pearson Addison-Wesley. All rights reserved September 5, 2007 Packages & Random and Math Classes ComS 207: Programming I (in Java) Iowa State University,
Programming Logic and Design Seventh Edition
Creating Objects & String Class
Section 11.1 Class Variables and Methods
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
METHODS AND BEHAVIORS AKEEL AHMED.
Chapter 6 Methods: A Deeper Look
Object-Oriented Programming Using C++ Second Edition
Programming games Classes and objects (used for Jigsaw, Bouncing stuff, other projects) Homework: Complete cannonball. Video or Audio. Your own project.
Packages & Random and Math Classes
Classes, Objects and Methods
Outline Creating Objects The String Class The Random and Math Classes
Chap 2. Identifiers, Keywords, and Types
String Objects & its Methods
Presentation transcript:

What is an object?

What Makes an Object? An object has identity (it acts as a single whole). Every object has a name that identifies what it is. Ex. The file object An object has state (it has various properties, which might change). Properties are often referred to as attributes. An object has behavior (it can do things and can have things done to it).

Software Objects Software objects will have identity, state, and behavior just as do real world objects. Objects (real world and software) have identity, state, and behavior. Software objects have identity because each is a separate chunk of memory (just like a yellow tennis ball, a software object is a distinct individual even if it looks nearly the same as other objects.) Software objects have state. Some of the memory that makes a software object is used for variables which contain values. Software objects have behavior. Some of the memory that makes a software object is used to contain programs (called methods/functions) that enable the object to "do things." The object does something when one of its method runs.

An Object

Class When a ACTIONSCRIPT application is being run, objects are created and their methods are invoked (are run.) To create an object, there needs to be a description of it. A class is a description of a kind of object. A programmer may define a class using ACTIONSCRIPT, or may use predefined classes that come in class libraries.

Example Here is a tiny application that instantiates (creates) an object by following the plan in the class String: var str1:String; // str1 is a variable that refers to an object, but the object does not exist yet. var len:Number; // len is a primitive variable of type Number (all data types are objects in AS3 even Number) str1 = "Random Jottings"; // create an object of type String len = str1.length; // invoke the object's property length Alert.show("The string is " + len + " characters long");

Notes The class String is defined at the Top Level and therefore does not need to be included into a program Class reference: reference/actionscript/3/String.html reference/actionscript/3/String.html

Objects and Names for Objects A variable that can refer to an object does not always have an object to refer to. For example, in our program the variable str1 refers to an object only after assigning the String to it. var str1:String; // str1 is a variable that refers to an object, // but the object does not exist yet var len:Number; // len is a primitive variable of type str1 = "Random Jottings"; // create an object of type String len = str1.length; // invoke the object's property length Alert.show("The string is " + len + " characters long"); Before (str1 = "Random Jottings"; ), str1 was a "place holder" that did not actually refer to any object.

Using a Reference to an Object Once the object has been created (with the new operator or in the case of a String, by just assigning it), the variable str1 refers to an existing object. That object has several properties, one of them the length property. A String object's Length properties stores the number of characters in the string. Reference: erence/actionscript/3/String.html erence/actionscript/3/String.html

Using an Object's Properties Remember that an object consists of both variables (state information) and methods (recipies for behavior.) Both of these are called "members" of the object. ACTIONSCRIPT uses "dot notation" for both: referenceToAnObject.memberOfObject For example, to invoke the Length property of the object named str1 the following is used: len = str1.length; Method names have "()" at their end.

Using a Class to Make Many Objects A class is like a cookie cutter that can be used many times to make many cookies. There is only one cookie cutter, but can be used to make many cookies. Cookies are objects and each one has its own individuality because each one is made out of a different section of dough. Different cookies may have different characteristics, even though they follow the same basic pattern. For example, different cookies can have different candy sprinkles or frosting, or be backed for different times. Cookies can be created. And cookies can be destroyed (just ask Cookie Monster). But destroying cookies does not affect the cookie cutter. It can be used again if there is dough left.

Static In the ACTIONSCRIPT language, a characteristic of a class definition that is not shared by its objects is called static. There is only one class definition for a given class, so when a program is running, if something is static then there is only one of it. You can think of the word static as meaning "no matter how many objects are made, there will be only one of these." For example: public static var specialX:Number;  this means that specialX is available to all objects derived from the class in which this variable is defined but is only accessible through the class itself! An example is the Math class and its list of static properties and functions Reference: ath.html ath.html

Static variables Static variables are declared using a combination of the static keyword and either the var or const statement. Static variables, which are attached to a class rather than an instance of a class, are useful for storing and sharing information that applies to an entire class of objects. For example, a static variable is appropriate if you want to keep a tally of the number of times a class is instantiated or if you want to store the maximum number of class instances that are allowed. The following example creates a totalCount variable to track the number of class instantiations and a MAX_NUM constant to store the maximum number of instantiations. The totalCount and MAX_NUM variables are static, because they contain values that apply to the class as a whole rather than to a particular instance. class StaticVars { public static var totalCount:int = 0; public static const MAX_NUM:uint = 16; } Code that is external to the StaticVars class and any of its subclasses can reference the totalCount and MAX_NUM properties only through the class itself. For example, the following code works: trace(StaticVars.totalCount); // output: 0 trace(StaticVars.MAX_NUM); // output: 16 You cannot access static variables through an instance of the class, so the following code returns errors: var myStaticVars:StaticVars = new StaticVars(); trace(myStaticVars.totalCount); // error trace(myStaticVars.MAX_NUM); // errorVariables that are declared with both the static and const keywords must be initialized at the same time as you declare the constant, as the StaticVars class does for MAX_NUM. You cannot assign a value to MAX_NUM inside the constructor or an instance method. The following code will generate an error, because it is not a valid way to initialize a static constant: // !! Error to initialize static constant this way class StaticVars2 { public static const UNIQUESORT:uint; function initializeStatic():void { UNIQUESORT = 16; } } Instance variables Instance variables include properties declared with the var and const keywords, but without the static keyword. Instance variables, which are attached to class instances rather than to an entire class, are useful for storing values that are specific to an instance. For example, the Array class has an instance property named length, which stores the number of array elements that a particular instance of the Array class holds.

Constructors One way in which objects are created is when the new operator is used with a constructor var str1:Info; // str1 is a reference to an object.(we’ll assume from now on the Info class is related to a String class var len:Number; str1 = new Info(“Random Jottings");//create an object of type Info len = str1.length; // invoke the object's method length() Alert.show("The string is " + String(len) + " characters long"); A constructor has the same name as the class Constructors often are used with values (called parameters) There are usually several different constructors in a class, each with different parameters (in AS3 there is only ONE constructor with multiple optional parameters). Sometimes one is more convenient to use than another, depending on how the new object's data is to be initialized.

The String Constructor Documentation Constructor Detail String () Constructor public function String(val:String)String Language Version: ActionScript 3.0 Runtime Versions: Flash Player 9, AIR 1.0, Flash Lite 4 Creates a new String object initialized to the specified string. Note: Because string literals use less overhead than String objects and are generally easier to use, you should use string literals instead of the String class unless you have a good reason to use a String object rather than a string literal. Parameters val:String — The initial value of the new String objectString Reference: /String.html

Dot Notation After an object has been constructed it can (usually) be changed by using its own methods (not its constructor.) Some objects are designed so that once they have been constructed they cannot be changed at all (concept of immutability).

Practice Program Fill in the blanks to: Create two Info objects. The first Info will get the characters "Green eggs" The second Info will get the characters " and ham." The program will compute the length of each Info, and then Write out the combined length of both Info (the sum of the individual lengths.) var str1:Info; // str1 is a reference to a Info object. var str2:Into; // str2 is a reference to a second Info object. var len1, len2 :Number; // the length of str1 and the length of str2 str1 = new Info(“Green eggs”) ; // create the first Info str2 = new Info(“ and ham.”) ; // create the second Info len1 = str1.length(); // get the length of the first string len2 = str2.length(); // get the length of the second string Alert.show("The combined length of both strings is " + (len1 + len2) + " characters" );

Quiz 1. What attributes do all real world objects have? a. Objects have identity, state, and behavior. b. Objects have state and behavior. c. Objects have size and weight. d. Objects have existence. 2. What attributes do all Software objects have? a. Software objects have RAM, ROM, and processors. b. Software objects have identity, state, and behavior. c. Software objects have variables and storage. d. Software objects are made of computer components.

3. What is a class? a. A class is a section of computer memory containing objects. b. A class is a section of the hard disk reserved for object oriented programs. c. A class is the part of an object that contains the variables. d. A class is a description of a kind of object. 4. What is another name for creating an object? a. instantiation b. insubordination c. initialization d. inheritance

5. What are the static variables and methods of a class? a. Variables and methods that form the foundation of each object of that class. b. Variables and methods that belong to all objects in the computer system. c. Variables and methods that belong only the objects of that class. d. Variables and methods that are part of the class definition, but not of its objects. 6. Which of the following invokes the method length() of the object str and stores the result in val? a. val = str.length() ; b. val = length.str() ; c. val = length().str ; d. val = length( str ) ;

7. How many objects of a given class may be constructed in an application? a. Only one per constructor. b. As many as the application asks for. c. Only one per class. d. One object per variable. 8. Which of the following is correct? a. String alpha("Hello Quiz!") ; b. String = "Hello Quiz!" ; c. String alpha = new "Hello Quiz!" ; d. var alpha:String = "Hello Quiz!" ;