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 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
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 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:
Multiple Choice Solutions True/False a c b e d   T F.
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.
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 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-4: Static Methods and Fields.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
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
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
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.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Static Data; More Inheritance reading:
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Building Java Programs Chapter 8 Lecture 8-3: Object state; Homework 8 (Critters) reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Copyright 2009 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-3: More Critters, static.
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2009 by Pearson Education Building Java Programs Chapter 9: Inheritance and Interfaces Lecture 9-1.
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,
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
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
Building Java Programs
Building Java Programs
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
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Homework 8: Critters (cont.)
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
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
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 the keyword this multiple constructors static fields and methods in a class

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

Copyright 2006 by Pearson Education 4 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 5 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 6 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 7 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 8 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 9 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; }... }

Copyright 2006 by Pearson Education 10 Static fields / methods

Copyright 2006 by Pearson Education 11 Static fields vs. fields static: Part of a class, rather than part of an object. Classes can have static fields. Unlike fields, static fields are not replicated into each object; instead a single field is shared by all objects of that class. static field, general syntax: private static ; or, private static = ; Example: private static int count = 0;

Copyright 2006 by Pearson Education 12 Static field example Count the number of Husky objects created: public class Husky implements Critter { // count of Huskies created so far private static int objectCount = 0; private int number; // each Husky has a number public Husky() { objectCount++; number = objectCount; }... public String toString() { return "I am Husky #" + number + "out of " + objectCount; }

Copyright 2006 by Pearson Education 13 Static methods static method: One that's part of a class, not part of an object. good places to put code related to a class, but not directly related to each object's state shared by all objects of that class does not understand the implicit parameter; therefore, cannot access fields directly if public, can be called from inside or outside the class Declaration syntax: (same as we have seen before) public static ( ) { ; }

Copyright 2006 by Pearson Education 14 Static method example 1 Java's built-in Math class has code that looks like this: public class Math {... public static int abs(int a) { if (a >= 0) { return a; } else { return -a; } public static int max(int a, int b) { if (a >= b) { return a; } else { return b; }

Copyright 2006 by Pearson Education 15 Static method example 2 Adding a static method to our Point class: public class Point {... // Converts a String such as "(5, -2)" to a Point. // Pre: s must be in valid format. public static Point parse(String s) { s = s.substring(1, s.length() - 1); // "5, -2" s = s.replaceAll(",", ""); // "5 -2" // break apart the tokens, convert to ints Scanner scan = new Scanner(s); int x = scan.nextInt(); // 5 int y = scan.nextInt(); // 2 Point p = new Point(x, y); return p; }

Copyright 2006 by Pearson Education 16 Calling static methods, outside Static method call syntax (outside the class):. ( ); This is the syntax client code uses to call a static method. Examples: int absVal = Math.max(5, 7); Point p3 = Point.parse("(-17, 52)");

Copyright 2006 by Pearson Education 17 Calling static methods, inside Static method call syntax (inside the class): ( ); This is the syntax the class uses to call its own static method. Example: public class Math { // other methods such as ceil, floor, abs, etc. //... public static int round(double d) { if (d - (int) d >= 0.5) { return ceil(d); } else { return floor(d); }