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.

Slides:



Advertisements
Similar presentations
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Advertisements

1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Chapter 4: Writing Classes
Chapter 3, More on Classes & Methods Clark Savage Turner, J.D., Ph.D. Copyright 2003 CSTurner, from notes and text.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
COMP 14 Introduction to Programming Miguel A. Otaduy May 25, 2004.
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Enhancing classes Visibility modifiers and encapsulation revisited
ECE122 L16: Class Relationships April 3, 2007 ECE 122 Engineering Problem Solving with Java Lecture 16 Class Relationships.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Classes, Encapsulation, Methods and Constructors
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 8, 2005.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley John Lewis, Peter DePasquale, and Joseph Chase Chapter 5: Writing Classes.
Writing Classes (Chapter 4)
The this Reference The this reference, used inside a method, refers to the object through which the method is being executed Suppose the this reference.
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
© 2004 Pearson Addison-Wesley. All rights reserved November 9, 2007 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
6-1 Object-Oriented Design Today we focuses on: –the this reference (Chapter 7) –the static modifier (Chapter 7) –method overloading (Chapter 7)
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.
CSE 1302 Lecture 7 Object Oriented Programming Review Richard Gesick.
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
© 2004 Pearson Addison-Wesley. All rights reserved November 7, 2007 Interfaces ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Object-Oriented Design Concepts
Java Software Solutions Lewis and Loftus Chapter 4 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects and Classes -- Introduction.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 6: Object-Oriented Design.
Chapter 6 Object-Oriented Design. © 2004 Pearson Addison-Wesley. All rights reserved6-2 Object-Oriented Design Now we can extend our discussion of the.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William.
Static class members.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
1 Object-Oriented Design Now we can extend our discussion of the design of classes and objects Chapter 6 focuses on: software development activities determining.
Chapter 6 Object-Oriented Design Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/20 The this Reference The this reference allows an object.
© 2004 Pearson Addison-Wesley. All rights reserved April 14, 2006 Polymorphism ComS 207: Programming I (in Java) Iowa State University, SPRING 2006 Instructor:
SEEM Java – Basic Introduction, Classes and Objects.
© 2004 Pearson Addison-Wesley. All rights reserved October 31, 2007 Static Class Members ComS 207: Programming I (in Java) Iowa State University, FALL.
Methods.
© 2004 Pearson Addison-Wesley. All rights reserved November 12, 2007 Inheritance ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
© 2004 Pearson Addison-Wesley. All rights reserved November 14, 2007 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, FALL.
© 2004 Pearson Addison-Wesley. All rights reserved April 5, 2006 Method Design & Method Overloading ComS 207: Programming I (in Java) Iowa State University,
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
University of Turkish Aeronautical Association Computer Engineering Department CENG 112 COMPUTER PROGRAMMING 2 Tansel Dökeroglu, Ph.D.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter VII: Arrays.
Chapter 5: Enhancing Classes
Interfaces November 6, 2006 ComS 207: Programming I (in Java)
Chapter 4: Writing Classes
Methods.
Passing Objects to Methods
More Object Oriented Programming
Object Oriented Programming
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Classes, Encapsulation, Methods and Constructors (Continued)
Chapter 5: Enhancing Classes
Chapter 4 Writing Classes.
Static Class Members March 29, 2006 ComS 207: Programming I (in Java)
CS139 October 11, 2004.
Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Object Oriented Programming Review
Object-Oriented Design Part 2
Presentation transcript:

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 and methods of a Java class. 2

3 The this Reference The this reference allows the methods of an object to refer to the object's own members. The this reference, used inside a method, refers to the object through which the method is being executed. Usually not necessary, but makes the intent more clear.

4 The this Reference Suppose the this reference is used in a method called tryMe, tryMe invoked as follows: obj1.tryMe(); obj2.tryMe(); In the first invocation, the this reference refers to obj1 ; in the second it refers to obj2 private int num; public void tryMe(){ this.num++; }

5 Before obj1 5 num obj2 2 num obj1.tryMe(); private int num; public void tryMe(){ this.num++; }

6 After obj1 6 num obj2 2 num obj1.tryMe(); private int num; public void tryMe(){ this.num++; }

7 After obj1 6 num obj2 3 num obj2.tryMe(); private int num; public void tryMe(){ this.num++; }

8 The this reference The this reference can be used to distinguish the instance variables of a class from corresponding method parameters with the same names. For example, the Account class (from Chapter 4) has three instance variables: private String name; private long acctNumber; private double balance;

9 The constructor was originally defined as: The constructor can be modified using this reference public Account (String name, long acctNumber, double balance) { this.name = name; this.acctNumber = acctNumber; this.balance = balance; } public Account (String owner, long account, double initial) { name = owner; acctNumber = account; balance = initial; }

Exercise Modify the Dog class constructor using the this reference. Download to a test directory the two files in Downloads/2016_03_21_Writing_Classes/ Downloads/2016_03_21_Writing_Classes/ Dog.java Kennel.java 10

//************************************************ // Dog.java // // Represents a dog. // //************************************************ public class Dog { // Instance variables private String name; private String breed; private int age; // // Constructor - sets up a dog object by initializing // the name, the breed, and the age. // public Dog(String newName, String newBreed, int newAge) { name = newName; breed = newBreed; age = newAge; } 11

Build and Run 12

Revised Constructor 13

Build and Run 14 Whoa! What happened?

When a Method is Called 15 The arguments in the call are copied into the parameters of the method. The method body is executed. The parameters are local variables in the method. In this case, the parameters shadow the member variables with the same names.

How to Avoid Shadowing The this reference permits us to refer to member variables having the same names as method parameters. 16

Build and Run 17 Now works as intended.

18 The static Modifier

19 Static Class Members Recall that a static method is one that can be invoked through its class name, instead of through an object of that class For example, the methods of the Math class are static: result = Math.sqrt(25) Variables can be static as well Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

20 Static Class Members Determining if a method or variable should be static is an important design decision. The methods in the Math class perform computations based on values passed as parameters. There is no good reason to force us to create an object in order to request these services.

21 The static Modifier We declare static methods and variables using the static modifier. It associates the method or variable with the class rather than with an object of that class Recall that the main method is static It is invoked by the Java interpreter without creating an object.

22 Static Methods public class Helper { public static int cube (int num) { return num * num * num; } Because it is declared as static, the method can be invoked as value = Helper.cube(3);

23 Static Variables Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists. private static float price; Memory space for a static variable is created when the class is first referenced.

24 Static Variables All objects instantiated from the class share its static variables. Changing the value of a static variable in one object changes it for all others.

25 Static Class Members The order of the modifiers can be interchanged, but by convention visibility modifiers come first Static methods cannot reference instance variables because instance variables don't exist until an object exists A static method can reference static member variables and its own local variables.

26 Static Class Members Static methods and static variables often work together. The following example keeps track of how many Slogan objects have been created using a static variable, and makes that information available using a static method.

27 Slogan.java public class Slogan { private String phrase; private static int count = 0; // // Constructor: Sets up the slogan and counts the number of // instances created. // public Slogan (String str) { phrase = str; count++; } // // Returns this slogan as a string. // public String toString() { return phrase; } // // Returns the number of instances of this class that have been // created. // public static int getCount () { return count; }

28 SloganCounter.java public class SloganCounter { // // Creates several Slogan objects and prints the number of // objects that were created. // public static void main (String[] args) { Slogan obj1, obj2, obj3, obj4, obj5; obj1 = new Slogan ("Remember the Alamo."); System.out.println (obj1); obj2 = new Slogan ("Don't Worry. Be Happy."); System.out.println (obj2); obj3 = new Slogan ("Live Free or Die."); System.out.println (obj3); obj4 = new Slogan ("Talk is Cheap."); System.out.println (obj4); obj5 = new Slogan ("Write Once, Run Anywhere."); System.out.println (obj5); System.out.println(); System.out.println ("Slogans created: " + Slogan.getCount()); }

Program Running 29

Question Can we call that static method getCount using an object rather than the class? Let's try it. Add at end of SloganCounter.java: System.out.println("obj1 says Slogans created: " + obj1.getCount()); System.out.println("obj2 says Slogans created: " + obj2.getCount()); System.out.println("obj3 says Slogans created: " + obj3.getCount()); System.out.println("obj4 says Slogans created: " + obj4.getCount()); System.out.println("obj5 says Slogans created: " + obj5.getCount()); 30

Test the Modified Program 31

Answer Yes, we can call static methods using an object as well as using the class name. 32

Exercise Modify the Dog class such that it contains static variable(s) and static method(s) to keep track of how many dog objects were created and make the information available in the driver (Kennel) program. 33

Dog.java public class Dog { private static int Dog_Count = 0; // Instance variables private String name; private String breed; private int age;... public static int Get_Dog_Count() { return Dog_Count; } 34

Kennel.java // Print summary for the dog System.out.println(dog1); System.out.println("Number of dogs created: " + Dog.Get_Dog_Count()); } 35

Build and Run 36

37 Method Design: Overloading

38 Method Design: Overloading Method overloading is the process of giving a single method name multiple definitions It is useful when you need to perform similar methods on different types of data. Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

39 Method Overloading The println method is overloaded: println (String s) println (int i) println (double d) and so on...

40 Method Overloading The following lines invoke different versions of the println method: System.out.println ("The total is:"); System.out.println (total);

41 Method Overloading If a method is overloaded, the method name is not sufficient to determine which method is being called. The signature of each overloaded method must be unique. The signature includes the number, type, and order of the parameters But not the return type.

42 Method Signature The signature of the following method includes 2 parameters, int and float, and the order of them (int first and float second) float tryMe(int x, float y) { return x*y; }

43 Method Overloading The compiler determines which method is to invoke by analyzing the parameters float tryMe(int x) { return x +.375; } float tryMe(int x, float y) { return x*y; } result = tryMe(25, 4.32) Invocation

44 Overloading Methods The return type of the method is not part of the signature. Overloaded methods cannot differ only by their return type. float tryMe(int x, float y) { return x*y; } int tryMe(int x, float y) { return (int)x*y; } result = tryMe(25, 4.32) Invocation Error!

45 Overloading Constructors Constructors can be overloaded We can have two more overloaded constructors in one class. Overloaded constructors provide multiple ways to initialize a new object of a given class.

46 Overloading Constructors public Account (String name, long acctNumber) { this.name = name; this.acctNumber = acctNumber; this.balance = 0; } public Account (String name, long acctNumber, double balance) { this.name = name; this.acctNumber = acctNumber; this.balance = balance; }

Exercise Write a class ComputeMax with two overloaded methods max One method takes two parameters and computes the maximum. The other takes three parameters and computes the maximum. 47

48 Readings and Assignments Not submitted or graded Reading: Chapter , Exercises and Programming Projects: After Chapter Exercises EX 7.1, 7.2, 7.3, 7.4, 7.5 Programming Projects PP 4.6, 4.7, 4.8 Exercise on previous slide. Assignment to be submitted and graded: Project 13