Download presentation
Presentation is loading. Please wait.
1
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 1 Chapter 7 More on Defining Classes Overloading methods and constructors Parameter Passing Objects as parameters this reference Class methods (static)
2
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 2 Overloaded Methods Methods can share the same name as long as –they have a different number of parameters (Rule 1) or –their parameters are of different data types when the number of parameters is the same (Rule 2) public void myMethod(int x, int y) {... } public void myMethod(int x) {... } public void myMethod(double x) {... } public void myMethod(int x) {... } Rule 1 Rule 2
3
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 3 Overloaded Constructors The same rules apply for overloaded constructors –this is how we can define more than one constructor to a class public Person( ) {... } public Person(int age) {... } public Pet(int age) {... } public Pet(String name) {... } Rule 1 Rule 2
4
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 4 Call-by-Value Parameter Passing When a method is called, –memory is allocated for each parameter variable –the value of the argument is copied into the matching parameter variable This way of passing the value of arguments is called a pass-by-value or call-by-value scheme. –the parameter is local to the method –changes to the parameter do not affect the value of the corresponding argument.
5
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 5 Call-by-Value Example class Tester { public void myMethod(int one, double two ) { one = 25; two = 35.4; } Tester tester; int x, y; tester = new Tester(); x = 10; y = 20; tester.myMethod(x, y); System.out.println(x + " " + y); produces 10 20
6
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 6 Memory Allocation for Parameters
7
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 7 Memory Allocation for Parameters (cont'd)
8
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 8 Objects as parameters For an object passed to a method, the location of the object is copied into the parameter variable. –The object is not copied. Changes made to the object persist when the method returns A new object created inside the method will not be seen from the calling method.
9
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 9 Parameter Passing: Key Points 1. Arguments are passed to a method by using the pass-by- value scheme. 2. Arguments are matched to the parameters from left to right.The data type of an argument must be assignment-compatible with the data type of the matching parameter. 3. The number of arguments in the method call must match the number of parameters in the method definition. 4. Parameters and arguments do not have to have the same name. 5. Local copies, which are distinct from arguments,are created even if the parameters and arguments share the same name. 6. Parameters are input to a method, and they are local to the method.Changes made to the parameters will not affect the value of corresponding arguments.
10
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 10 Fraction class aka RationalNumber class To represent a fraction, need –numerator –denominator (should never be 0) Operations needed –accessors –mutators –simplify –arithmetic
11
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 11 Returning an Object from a Method Methods can return objects as well as primitive values What gets returned is a reference (or an address) to the object. –The object is not copied
12
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 12 Sample Object-Returning Method Here's a sample method that returns an object: public Fraction simplify( ) { Fraction simp; int num = getNumerator(); int denom = getDenominator(); int gcd = gcd(num, denom); simp = new Fraction(num/gcd, denom/gcd); return simp; } Return type indicates the class of an object we're returning from the method. Return an instance of the Fraction class
13
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 13 A Sample Call to simplify
14
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 14 A Sample Call to simplify (cont'd)
15
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 15 Reserved Word this How does a method know where to find the data for the object it is called with? –The reserved word this is a reference to that object. this is called a self-referencing pointer because it refers to an object from that object's method. : Object this
16
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 16 Uses of this The reserved word this can be used in several different ways. – To access other methods and instance variables within an instance method This use is usually optional. It is implied in a method call that is not qualified with an object name this can be used to disambiguate an instance variable from a local variable –To allow a constructor to call a different constructor –To allow a method to pass a reference to its object to another method We'll see examples when we start looking at the GUI classes
17
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 17 Using this to Refer to Other Members Methods public class Fraction { private int numerator, denominator; public void simplify() { int num, den, gcd; num = this.getNumerator(); den = this.getDenominator(); gcd = gcd( num, den); return new Fraction( num/gcd, den/gcd); }... } Data members public class Person { int age; public void setAge(int val) { this.age = val; }... }
18
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 18 f1.add(f2) f2.add(f1) This time, we're calling f2's method, so the reserved word this is referring to f2. Because f1 is the receiving object (we're calling f1's method), so the reserved word this is referring to f1.
19
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 19 Using this to Disambiguate Consider the following class definition public class Fraction { private int numerator, denominator; public Fraction( int numerator, int denominator) { this.numerator = numerator; this.denominator = denominator; }... } Without the this, all references to numerator and denominator would be to the parameter variables
20
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 20 Constructors and this Use this to call a constructor from another constructor of the same class Must be the first statement in the constructor public Fraction( ) { //creates 0/1 this(0, 1); } public Fraction(int number) { //creates number/1 this(number, 1); } public Fraction(Fraction frac) { //copy constructor this(frac.getNumerator(), frac.getDenominator()); } public Fraction(int num, int denom) { setNumerator(num); setDenominator(denom); }
21
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 7 - 21 Class Methods We use the reserved word static to define a class method. public static int gcd(int m, int n) { //the code implementing the Euclidean algorithm } public static Fraction min(Fraction f1, Fraction f2) { //convert to decimals and then compare } public static Photo readPhoto(Scanner in) { //the code that reads data and returns a Photo } From a different class, static methods are called using the name of the class instead of the name of an object.
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.