Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Encapsulation, this, Subclasses.
1 Building Java Programs Chapter 8: Classes These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-1: Intro to Classes and Objects reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Packages. Package A package is a set of related classes Syntax to put a class into a package: package ; public class { …} Two rules:  A package declaration.
Based on slides at buildingjavaprograms.com Objects and Classes, take 2 Managing Complexity with Programmer-Defined Types  Classes are programmer-defined.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 19: encapsulation, inheritance reading: (Slides adapted from Stuart.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
CS 112 Introduction to Programming User-Defined Data Types Yang (Richard) Yang Computer Science Department Yale University 308A Watson, Phone:
1 Object state: fields. Fields field: A variable inside an object that represents part of its internal state.  Each object will have its own copy of.
CSCI 162 Classes.
CSC 142 Computer Science II Zhen Jiang West Chester University
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Object Oriented Programming
Building Java Programs
Lecture 11: More on Classes
CSC240 Computer Science III
Building Java Programs Chapter 8
Classes and Objects, State and Behavior
Lecture 8-3: Encapsulation, this
Building Java Programs
Object initialization: constructors
Topic 28 classes and objects, part 2
Building Java Programs
Topic 29 classes and objects, part 3
Building Java Programs
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Lecture 12: Classes II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects
Building Java Programs
Building Java Programs
Lecture 11: Intro to Classes
Kinds of methods accessor: A method that lets clients examine object state. Examples: distance, distanceFromOrigin often has a non-void return type mutator: A.
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Special instance methods: toString
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Building Java Programs Chapter 8
reading: 8.6 self-check: #18, exercises: #9, 14
Building Java Programs
Topic 29 classes and objects, part 3
Building Java Programs
Topic 29 classes and objects, part 3
Presentation transcript:

Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects

Copyright 2006 by Pearson Education 2 Lecture outline advanced classes the toString method the keyword this

Copyright 2006 by Pearson Education 3 The toString method reading: 8.6

Copyright 2006 by Pearson Education 4 Printing objects By default, Java doesn't know how to print objects: Point p = new Point(10, 7); System.out.println("p is " + p); // p is We can print a better string (but this is cumbersome): System.out.println("(" + p.x + ", " + p.y + ")"); We'd like to be able to print the object itself: // desired behavior System.out.println("p is " + p); // p is (10, 7)

Copyright 2006 by Pearson Education 5 The toString method The special method toString : Tells Java how to convert your object into a String as needed. Is called when an object is printed or concatenated to a String. Point p1 = new Point(7, 2); System.out.println("p1 is " + p1); If you prefer, you can write the.toString() explicitly. System.out.println("p1 is " + p1.toString()); Every class has a toString, even if it isn't in your code. The default toString returns the class's name followed by a hexadecimal (base-16) number:

Copyright 2006 by Pearson Education 6 toString method syntax You can replace the default behavior by defining a toString method in your class. public String toString() { ; } Example: // Returns a String representing this Point. public String toString() { return "(" + x + ", " + y + ")"; }

Copyright 2006 by Pearson Education 7 Client code question Recall our client program that produces this output: p1 is (7, 2) p1's distance from origin = p2 is (4, 3) p2's distance from origin = 5.0 p1 is (18, 8) p2 is (5, 10) distance from p1 to p2 = 13.0 Modify the program to use our new toString method.

Copyright 2006 by Pearson Education 8 Client code answer // This client program uses the Point class. public class PointMain { public static void main(String[] args) { // create two Point objects Point p1 = new Point(7, 2); Point p2 = new Point(4, 3); // print each point System.out.println("p1 is " + p1); System.out.println("p2 is " + p2); // compute/print each point's distance from the origin System.out.println("p1's distance from origin = " + p1.distanceFromOrigin()); System.out.println("p2's distance from origin = " + p1.distanceFromOrigin()); // move p1 and p2 and print them again p1.translate(11, 6); p2.translate(1, 7); System.out.println("p1 is " + p1); System.out.println("p2 is " + p2); // compute/print distance from p1 to p2 System.out.println("distance from p1 to p2 = " + p1.distance(p2)); }

Copyright 2006 by Pearson Education 9 The keyword this reading: 8.7

Copyright 2006 by Pearson Education 10 Using the keyword this this : A reference to the implicit parameter. implicit parameter: object on which a method/constructor is called this keyword, general syntax: To refer to a field: this. To call a method: this. ( ); To call a constructor from another constructor: this( );

Copyright 2006 by Pearson Education 11 Variable names and scope Usually it is illegal to have two variables in the same scope with the same name. Recall: Point class's setLocation method: Params named newX and newY to be distinct from fields x and y public class Point { int x; int y;... public void setLocation(int newX, int newY) { if (newX < 0 || newY < 0) { throw new IllegalArgumentException(); } x = newX; y = newY; } }

Copyright 2006 by Pearson Education 12 Variable shadowing However, a class's method can have a parameter whose name is the same as one of the class's fields. Example: // this is legal public void setLocation(int x, int y) {... } Fields x and y are shadowed by parameters with same names. Any setLocation code that refers to x or y will use the parameter, not the field. shadowed variable: A field that is "covered up" by a parameter or local variable with the same name.

Copyright 2006 by Pearson Education 13 Avoiding shadowing with this The keyword this prevents shadowing: public class Point { private int x; private int y;... public void setLocation(int x, int y) { if (x < 0 || y < 0) { throw new IllegalArgumentException(); } this.x = x; this.y = y; } Inside the setLocation method: When this.x is seen, the field x is used. When x is seen, the parameter x is used.

Copyright 2006 by Pearson Education 14 Multiple constructors It is legal to have more than one constructor in a class. The constructors must accept different parameters. public class Point { private int x; private int y; public Point() { x = 0; y = 0; } public Point(int initialX, int initialY) { x = initialX; y = initialY; }... }

Copyright 2006 by Pearson Education 15 Multiple constructors w/ this One constructor can call another using this We can also rename the parameters and use this. field syntax. public class Point { private int x; private int y; public Point() { this(0, 0); // calls the (x, y) constructor } public Point(int x, int y) { this.x = x; this.y = y; }... }