CSSE501 Object-Oriented Development. Chapter 5: Messages, Instances and Initialization  This chapter examines OOP run-time features: Object Creation.

Slides:



Advertisements
Similar presentations
Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Advertisements

Python Objects and Classes
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Computer Science and Engineering College of Engineering The Ohio State University Classes and Objects: Members, Visibility The credit for these slides.
Chapter 7: User-Defined Functions II
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
1 Objects and ClassesStefan Kluth 1.6Objects and Classes 1.6What is an Object? 1.7Objects and Classes 1.8Object Interface, Class Inheritance, Polymorphism.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
Road Map Introduction to object oriented programming. Classes
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Ch 4. Memory Management Timothy Budd Oregon State University.
OOP Languages: Java vs C++
Classes CMPS Overview Terms Class vs instance Class definitions in various languags Access modifiers Methods Constants Separating definition and.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Messages, Instances, and Initialization (Methods) CMPS 2143.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CSSE501 Object-Oriented Development. Chapter 12: Implications of Substitution  In this chapter we will investigate some of the implications of the principle.
CSCI-383 Object-Oriented Programming & Design Lecture 13.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
CSC241 Object-Oriented Programming (OOP) Lecture No. 6.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CS 1704 Introduction to Data Structures and Software Engineering.
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
1 COSC2767: Object-Oriented Programming Haibin Zhu, Ph. D. Professor of Computer Science, Nipissing University.
CSI 3125, Preliminaries, page 1 Class. CSI 3125, Preliminaries, page 2 Class The most important thing to understand about a class is that it defines a.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Lecture 4 : Data Abstraction with C++ : class
Object-Oriented Programming (OOP) What we did was: (Procedural Programming) a logical procedure that takes input data, processes it, and produces output.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
YG - CS Concept of Encapsulation What is encapsulation? - data and functions/methods are packaged together in the class normally.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Object-Oriented Design Chapter 7 1. Objectives You will be able to Use the this reference in a Java program. Use the static modifier for member variables.
Memory Management in Java Mr. Gerb Computer Science 4.
Object Lifetimes and Dynamic Objects Lecture-7. Object Lifetimes and Dynamic Objects External (Global) Objects Persistent (in existence) throughout the.
Polymorphic Variables CMPS2143. The Polymorphic Variable A polymorphic variable is a variable that can reference more than one type of object (that is,
Constructors and Destructors
Data Types In Text: Chapter 6.
Topic: Classes and Objects
Overview 4 major memory segments Key differences from Java stack
Intro To Classes Review
University of Central Florida COP 3330 Object Oriented Programming
Object-Oriented Programming & Design Lecture 14 Martin van Bommel
Object Oriented Programming in java
Constructors and Destructors
9-10 Classes: A Deeper Look.
9-10 Classes: A Deeper Look.
Classes and Objects Object Creation
Corresponds with Chapter 5
Presentation transcript:

CSSE501 Object-Oriented Development

Chapter 5: Messages, Instances and Initialization  This chapter examines OOP run-time features: Object Creation and Initialization (constructors) Message Passing Syntax Accessing the Receiver from within a method Memory Management or Garbage Collection

Constructors  A constructor is a function that is implicitly invoked when a new object is created. The constructor performs whatever actions are necessary in order to initialize the object In C++, Java, C# a constructor is a function with the same name as the class. In Python constructors are all named __init__ In Delphi, Objective-C, constructors have special syntax, but can be named anything. (Naming your constructors create is a common convention) class PlayingCard { // a Java constructor public PlayingCard (int s, int r) { suit = s; rank = r; faceUp = true; }... }

Overloaded Constructors  Constructors are often overloaded, meaning there are a number of functions with the same name. They are differentiated by the type signature, and the arguments used in the function call or declaration: //C++ example class Student { private: int idNum; string name; double gradePointAverage; public: Student(); Student(int); Student(int, string); Student(int, string, double); …… }; Student::Student() {idNum = 9999; name = “J. Smith”; gradePointAverage = 0.0; } Student::Student(int id) {idNum = id; } …… Student::Student(int id, string nm, double grade) {idNum = id; name = nm; gradePointAverage = grade; }

//Java Example class DigitalCounter { private int counter; private int minimum; private int maximum; public DigitalCounter() {counter = 0; minimum = 0; maximum = 9;} public DigitalCounter(int counter, int minimum, int maximum) { this.counter = counter; this.minimum = minimum; this.maximum = maximum; } //return the current counter value public int getCounter() { return counter; } public int getMinimum() { return minimum; } public int getMaximum() { return maximum; } public void setCounter(int counter) { this.counter = counter;} public void setMinimum(int minimum) {this.minimum = minimum;} public void setMaximum(int maximum) {this.maximum = maximum;} public void increaseCounter() { if (counter == maximum) counter = minimum; else counter = counter +1; }

Instances  Now, we could use constructors to create instances of classes  C++ Student oneStudent; Student twoStudent(7766, “Anna Moore”, 3.74);

Instances  Java //a digital counter DC1 with minimum 0, maximum 9, and //current counter value 0 DigitalCounter DC1 = new DigitalCounter(); //a digital counter DC2 with minimum 0, maximum 59, and //current counter value 5 DigitalCounter DC2 = new DigitalCounter(5, 0, 59);

Object Creation  In most programming languages objects must be created dynamically, usually using the new operator: PlayingCard aCard; // simply names a new variable aCard = new PlayingCard(Diamond, 3); // creates the new object  The declaration simply names a variable, the new operator is needed to create the new object value

Messages are not Function Calls  Recall from chapter 1 that we noted the following differences between a message and a function (or procedure) call A message is always given to some object, called the receiver The action performed in response is determined by the receiver, different receivers can do different actions in response to the same message

Message Passing Syntax  Although the syntax may differ in different languages, all messages have three identifiable parts: aGame.displayCard (aCard, 42, 27); The message receiver: aGame The message selector: displayCard An optional list of arguments: aCard, 42, 27

Statically Types and Dynamically Typed Languages  A distinction we will see throughout the term is between the following: A statically typed language requires the programmer to declare a type for each variable. The validity of a message passing expression will be checked at compile time, based on the declared type of the receiver (e.g., Java, C++, C#, Pascal). A dynamically typed language associates types with values, not with variables. A variable is just a name. The legality of a message cannot be determined until run-time (E.g., Smalltalk, CLOS, Python).

The Receiver Variable  Inside a method, the receiver can be accessed by means of a pseudo-variable Called this in Java, C++, C# Called self in Smalltalk, Objective-C, Object Pascal Called current in Eiffel function PlayingCard.color : colors; begin if (self.suit = Heart) or (self.suit = Diamond) then color := Red else color := Black; end

Implicit Use of This  Within a method a message expression or a data access with no explicit receiver is implicitly assumed to refer to this: class PlayingCard {... public void flip () { setFaceUp( ! faceUp ); }... } Is assumed to be equivalent to: class PlayingCard {... public void flip () { this.setFaceUp( ! this.faceUp); }... }

Memory Recovery  Because in most languages objects are dynamically allocated, they must be recovered at run-time. There are two broad approaches to this: Force the programmer to explicitly say when a value is no longer being used (e.g., C++, Delphi Pascal): delete aCard; // C++ example Use a garbage collection system that will automatically determine when values are no longer being used, and recover the memory (e.g., Java, C#, Smalltalk, CLOS).

Memory Errors  Garbage collection systems impose a run-time overhead, but prevent a number of potential memory errors: Running out of memory because the programmer forgot to free values Using a memory value after it has been recovered PlayingCard * aCard = new PlayingCard(Spade, delete aCard; cout << aCard.rank(); Free the same value twice PlayingCard * aCard = new PlayingCard(Spade, 1); delete aCard; delete aCard; // delete already deleted value