1 Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality.

Slides:



Advertisements
Similar presentations
Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality Passing.
Advertisements

Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Chapter 13 - Inheritance and Polymorphism The Object Class The equals Method The toString Method Polymorphism Dynamic Binding Compilation Details Polymorphism.
Defining classes and methods Recitation – 09/(25,26)/2008 CS 180 Department of Computer Science, Purdue University.
10-Jun-15 Just Enough Java. Variables A variable is a “box” that holds data Every variable has a name Examples: name, age, address, isMarried Variables.
CS 106 Introduction to Computer Science I 03 / 30 / 2007 Instructor: Michael Eckmann.
Programmer-defined classes Part 3. A class for representing Fractions public class Fraction { private int numerator; private int denominator; public Fraction(int.
Review of C++ Programming Part II Sheng-Fang Huang.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Java Fundamentals Expanded SE1021 Dr. Mark L. Hornick 1.
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality Passing.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
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.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 6 l Array Basics l Arrays and Methods l Programming with Arrays.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
Pointers *, &, array similarities, functions, sizeof.
SEEM Java – Basic Introduction, Classes and Objects.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
1 Class 1 Lecture Topic Concepts, Definitions and Examples.
CIT 590 Intro to Programming Lecture 13. Some Eclipse shortcuts CTRL + SHIFT + F – format file (proper indentation etc). Please do this before you submit.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Peyman Dodangeh Sharif University of Technology Spring 2014.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
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.
The const Keyword Extreme Encapsulation. Humble Beginnings There are often cases in coding where it is helpful to use a const variable in a method or.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Methods, classes, and Objects Dr. Jim Burns. Question  Which of the following access modifiers is the default modifier?  public  private  protected.
Java: Base Types All information has a type or class designation
Topic: Classes and Objects
User-Written Functions
Java: Base Types All information has a type or class designation
Some Eclipse shortcuts
Java Primer 1: Types, Classes and Operators
Chapter 3: Using Methods, Classes, and Objects
Constructors CMSC 202.
CS 302 Week 11 Jim Williams, PhD.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
CSC 113 Tutorial QUIZ I.
Object Oriented Programming in java
Java Programming Language
Just Enough Java 17-May-19.
References Revisted (Ch 5)
Classes and Objects Object Creation
Corresponds with Chapter 5
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
CMSC 202 Constructors Version 9/10.
Presentation transcript:

1 Chapter 7 Object-Oriented Programming – Additional Details Object Creation - a Detailed Analysis Assigning a Reference Testing Objects For Equality Passing References as Arguments Method-Call Chaining Overloaded Methods Constructors Overloaded Constructors 1

2 Object Creation - a Detailed Analysis Let's start the chapter with a behind-the-scenes detailed look at what happens when a program instantiates an object and stores its address in a reference variable. Code fragment: 1. Car car;// reference variable declaration 2. car = new Car();// object creation 3. car.year = 1998;// assign 1998 to car's year instance variable 1. Space is allocated in memory for the car reference variable. The car reference variable will hold the address of an object, but since there's no object created for it yet, it doesn't yet hold a legitimate address. 2. Space is allocated in memory for a new Car object. The address of the allocated space is assigned to car. 3. The car variable's value (the address of a Car object) is used to find the Car object in memory, and then 1998 can be stored in the Car object. Note that for this assignment to work, we're making the simplifying assumption that year is a public instance variable

3 Object Creation - a Detailed Analysis Code fragment: 1. Car car; 2. car = new Car(); 3. car.year = 1998; ?

4 Assigning a Reference The result of assigning one reference variable to another is that both reference variables then point to the same object. With both reference variables pointing to the same object, if the object is updated by one of the reference variables, then the other reference variable will notice that change when it attempts to access the object. That can be disconcerting! 1 2

5 Assigning a Reference Suppose you want to create two Car objects that are the same except for their color. Your plan is to create the first car, copy the first car to the second car, and then update the second car's color instance variable. Will this code accomplish that? Car stacyCar; Car johnCar = new Car(); johnCar.setMake("Honda"); johnCar.setYear(2003); johnCar.setColor("silver"); stacyCar = johnCar; stacyCar.setColor("peach"); 1

6 Assigning a Reference The problem with the previous slide's code is that the stacyCar = johnCar; statement causes the two references to point to the same single Car object. Thus, johnCar 's color becomes "peach" (and that was not intended). johnCar = new Car();... stacyCar = johnCar; stacyCar.setColor("peach"); 1

7 Assigning a Reference If you want to make a copy of a reference variable, you should not assign the reference to another reference. Instead, you should instantiate a new object for the second reference and then assign the two objects' instance variables one at a time. johnCar = new Car(); stacyCar = new Car(); 1

8 Assigning a Reference On the next slide, we make a copy of the johnCar reference variable by calling a makeCopy method. The makeCopy method implements the strategy outlined on the previous slide - it instantiates a new object and then copies instance variables into it one at a time. More specifically, the makeCopy method: Instantiates a local variable named car. Copies the calling object car's instance variables into the local variable car 's instance variables. Returns the local variable car to the calling module. 2 1

9 Assigning a Reference public static void main(String[] args) { Car johnCar = new Car(); Car stacyCar; johnCar.setMake("Honda"); johnCar.setYear(2003); johnCar.setColor("silver"); stacyCar = johnCar.makeCopy(); stacyCar.setColor("peach"); } // end main 1

10 Assigning a Reference public class Car { private String make; private int year; private String color;... public Car makeCopy() { Car car = new Car(); car.make = this.make; car.year = this.year; car.color = this.color; return car; } // end makeCarCopy } // end class Car

11 Testing Objects for Equality Using the == operator: When comparing two reference variables with ==, you'd probably expect == to return true if the data in the two reference variables is the same. Unfortunately, that's not how things work. For example, this prints "different": Car car1 = new Car(); car1.setColor("red"); Car car2 = new Car(); car2.setColor("red"); if (car1 == car2) { System.out.println("the same"); } else { System.out.println("different"); } The car1 == car2 expression returns false. Why? 1

12 Testing Objects for Equality Using the == operator (continued): The == operator returns true if the two reference variables point to the same object; i.e., the two reference variables contain the same address. For example, what does this code fragment print? Car car1 = new Car(); Car car2 = car1; if (car1 == car2) { System.out.println("the same"); } else { System.out.println("different"); } 1

13 Testing Objects for Equality Usually, the == operator is not good enough. You'll usually want to compare the contents of two objects rather than just whether two reference variables point to the same object. To do that, you'll need to have an equals method in the object's class definition that compares the contents of the two objects. 1

14 Testing Objects for Equality Write an equals method for a Car2 class. Use this skeleton: public class Car2 { private String make; private int year; private String color; } // end class Car2 public class Car2Driver { public static void main(String[] args) { Car2 simonCar = new Car2(); Car2 joeCar = new Car2();... if (simonCar.equals(joeCar)) { System.out.println("cars have identical features"); }

15 Testing Objects for Equality (hidden) Write an equals method for a Car2 class. Use this skeleton: public class Car2 { private String make; private int year; private String color; } // end class Car2 public class Car2Driver { public static void main(String[] args) { Car2 simonCar = new Car2(); Car2 joeCar = new Car2();... if (simonCar.equals(joeCar)) { System.out.println("cars have identical features"); } 1 2

16 Passing References as Arguments Suppose you pass a reference variable to a method, and inside the method you update the reference variable's instance variables. What happens? … Remember that a reference variable holds the address of an object, not the object itself. So in passing a reference variable argument to a method, a copy of the object's address (not a copy of the object itself) is passed to the method and stored in the method's parameter. Since the parameter and the argument hold the same address value, they point to the same object. Thus, if one of the parameter's instance variables is updated, then the update will simultaneously update the argument's instance variable in the calling module. 1 2

17 Passing References as Arguments public class PersonDriver { public static void main(String[] args) { Person person1 = new Person(); Person person2 = new Person(); person1.setName("Jonathan"); person2.setName("Benji"); System.out.println(person1.getName() + ", " + person2.getName()); person1.swapPerson(person2); System.out.println(person1.getName() + ", " + person2.getName()); } // end main } // end PersonDriver

18 Aside: Swapping algorithm Write a pseudocode fragment that swaps the contents of the x and y variables. More specifically, fill in the swap code below such that the output is "x=8, y=3". x  3 y  8 print "x=" + x + ", y=" + y

19 Passing References as Arguments public class Person { private String name; public void setName(String name) { this.name = name; } public String getName() { return this.name; } public void swapPerson(Person otherPerson) { String temp; temp = otherPerson.name; otherPerson.name = this.name; this.name = temp; } // end swapPerson } // end Person 1 2

20 Method-Call Chaining Up to this point, we've called methods one at a time. In an earlier example, we had a johnCar reference variable and we set its make and year like this: johnCar.setMake("Honda"); johnCar.setYear(2003); Let's now discuss how you can chain the two method calls together, like this: johnCar.setMake("Honda").setYear(2003); That's called method-call chaining. It's when you use a dot to concatenate a method call to the end of another method call. 1

21 Method-Call Chaining public class Car3Driver { public static void main(String[] args) { Car3 car = new Car3(); car.setMake("Honda").setYear(1998).printIt(); } // end main } // end class Car3Driver a method-call chain 1 2

22 Method-Call Chaining public class Car3 { private String make; private int year; //******************************************************* public Car3 setMake(String make) { this.make = make; return this; } // end setMake public Car3 setYear(int year) { this.year = year; return this; } // end setYear //******************************************************* public void printIt() { System.out.println(make + ", " + year); } // end printIt } // end class Car2 1 2 The return type is the same as the class name. Return the calling object.

23 Method-Call Chaining In Car3 's setMake and setYear methods, note how we enable method-call chaining. In each method definition: The last line in the method body returns the calling object: return this; The method heading specifies the method's class name for the return type: public Car3 setMake(String make); Method-call chaining is optional. So why bother with it? 1 2 3

24 Overloaded Methods Suppose there's a need to perform the same sort of task on different sets of arguments. For example, suppose you want to find the average for these different sets of arguments: two integers three integers two doubles One solution is to write three methods with three different names. Here's how you might call those methods: x = findAverageFor2Ints(20, 8); y = findAverageFor3Ints(5, -3, 18); z = findAverageFor2Doubles(1.2, 4.0); What's wrong with that solution? 1

25 Overloaded Methods The better solution is to use overloaded methods. That's where you have two or more methods with the same name and different parameters (different number of parameters or different types of parameters). For the find-the-average example, you could write three overloaded findAverage methods and call them like this: x = findAverage(20, 8); y = findAverage(5, -3, 18); z = findAverage(1.2, 4.0); 2 1 3

26 Overloaded Methods class Height { double height; // a person's height String units; // unit of measurement (e.g., cm for centimeters) public void setHeight(double height) { this.height = height; this.units = "cm"; } public void setHeight(double height, String units) { this.height = height; this.units = units; } public void print() { System.out.println(this.height + " " + this.units); } } // end class Height 1 2 Note that the overloaded setHeight methods have different numbers of parameters.

27 Overloaded Methods public class HeightDriver { public static void main(String[] args) { Height myHeight = new Height(); myHeight.setHeight(72.0, "in"); myHeight.print(); myHeight.setHeight(180.0); myHeight.print(); } // end main } // end class HeightDriver For each setHeight call, which method is called on the previous slide? What is the program's output? 1 2 3

28 Overloaded Methods Suppose that you have overloaded methods and you're inside one of the methods. Note that it's OK to call one of the other overloaded methods. For example, you can replace the original one- parameter setHeight method with the following implementation, which calls the two-parameter setHeight method. public void setHeight(double height) { setHeight(height, "cm"); } No need for a reference variable dot prefix here. 1 2

29 Constructors Up to this point, we have used mutators to assign values to the instance variables in newly instantiated objects. That works OK, but it requires having and calling one mutator for each instance variable. As an alternative, you could use a single method to initialize all of an object's instance variables as soon as possible after you create that object. For example, you could define a single initCar method to initialize Car objects and use it like this:: Car allexCar = new Car(); allexCar.initCar("Porsche", 2006, "beige"); This code fragment uses one statement to allocate space for a new object, and it uses another statement to initialize that object's instance variables. Since the instantiation and initialization of an object is so common, wouldn't it be nice if there were a single statement that could handle both of these operations? Car allexCar = new Car("Porsche", 2006, "beige"); 1 2

30 Constructors A constructor lets you specify what happens to an object when it is instantiated with new. A constructor is called automatically when an object is instantiated. A constructor's name = the object's class name. Don't put a type at the left of a constructor heading (because constructors never return anything)

31 Example Car Class with a Constructor public class Car4Driver { public static void main(String[] args) { Car4 jobyCar = new Car4("Porsche", 2006, "beige"); Car4 primeCar = new Car4("Saturn", 2002, "red"); System.out.println(jobyCar.getMake()); } // end main } // end class Car4Driver constructor calls 1 2

32 Example Car Class with a Constructor public class Car4 { private String make; // car's make private int year; // car's manufacturing year private String color; // car's primary color //**************************************************** public Car4(String m, int y, String c) { this.make = m; this.year = y; this.color = c; } // end constructor //**************************************************** public String getMake() { return this.make; } // end getMake } // end class Car4 constructor definition Style requirement: Put constructors above a class's methods

33 Constructors Any time you instantiate an object (with new ), there must be a matching constructor. That is, the number and types of arguments in your constructor call must match the number and types of parameters in a defined constructor. Until recently, we've instantiated objects without any explicit constructor. So were those examples wrong? The Java compiler automatically provides an empty- bodied zero-parameter default constructor for a class if and only if the class contains no explicitly defined constructors. The Employee program on the next slide illustrates the use of Java's implicit zero-parameter default constructor. 1 2

34 Will this program compile successfully? Will the next slide’s program compile successfully? import java.util.Scanner; public class Employee { private String name; public void readName() { Scanner stdIn = new Scanner(System.in); System.out.print("Name: "); this.name = stdIn.nextLine(); } // end readName } // end class Employee public class EmployeeDriver { public static void main(String[] args) { Employee emp = new Employee(); emp.readName(); } // end main } // end class EmployeeDriver 1 2

35 Will this program compile successfully? import java.util.Scanner; public class Employee2 { private String name; public Employee2(String n) { this.name = n; } // end constructor public void readName() { Scanner stdIn = new Scanner(System.in); System.out.print("Name: "); this.name = stdIn.nextLine(); } // end readName } // end class Employee2 public class Employee2Driver { public static void main(String[] args) { Employee2 waiter = new Employee2("Wen-Jung"); Employee2 hostess = new Employee2(); hostess.readName(); } // end main } // end class Employee2Driver 1 2

36 Overloaded Constructors Constructor overloading occurs when there are two or more constructors with the same name and different parameters. To call an overloaded constructor from another overloaded constructor, use this syntax: this( ); A this( ) constructor call may appear only in a constructor definition, and it must appear as the very first statement in the constructor definition. See the example on the next slide.…

37 Overloaded Constructors public class Fraction { private int numerator; private int denominator; private double quotient; public Fraction(int n) { this(n, 1); } public Fraction(int n, int d) { this.numerator = n; this.denominator = d; this.quotient = (double) this.numerator / this.denominator; } public void printIt() { System.out.println( this.numerator + " / " + this.denominator + " = " + this.quotient; } // end printIt } // end Fraction class public class FractionDriver { public static void main(String[] args) { Fraction a = new Fraction(3, 4); Fraction b = new Fraction(3); // Fraction c = new Fraction(); // error a.printIt(); b.printIt(); } // end main } // end class FractionDriver

38 Overloaded Constructors (hidden) public class Fraction { private int numerator; private int denominator; private double quotient; public Fraction(int n) { this(n, 1); } public Fraction(int n, int d) { this.numerator = n; this.denominator = d; this.quotient = (double) this.numerator / this.denominator; } public void printIt() { System.out.println( this.numerator + " / " + this.denominator + " = " + this.quotient; } // end printIt } // end Fraction class public class FractionDriver { public static void main(String[] args) { Fraction a = new Fraction(3, 4); Fraction b = new Fraction(3); // Fraction c = new Fraction(); // error a.printIt(); b.printIt(); } // end main } // end class FractionDriver

39 Chapter 7 - Quiz Questions 3.A constructor is automatically called whenever a)a Boolean variable is used b)an object is instantiated with new c)a method calls another method 2.Overloaded methods are methods that: a)have more parameters than the maximum number of parameters allowed b)share memory space with tendril instance variables c)have the same name and different parameters 1.What language uses pointers? a)Java b)C++ c)Visual Basic