©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 7 More on Defining Classes Overloading methods and constructors Parameter Passing Objects as parameters this reference Class methods (static)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Overloaded Constructors The same rules apply for overloaded constructors –this is how we can define more than one constructor to a class –The Student class has two constructors public Person( ) {... } public Person(int age) {... } public Pet(int age) {... } public Pet(String name) {... } Rule 1 Rule 2
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Memory Allocation for Parameters
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Memory Allocation for Parameters (cont'd)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Fraction class aka RationalNumber class To represent a fraction, need –numerator –denominator (should never be 0) Operations needed –accessors –mutators –simplify –arithmetic
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter A Sample Call to simplify
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter A Sample Call to simplify (cont'd)
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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; }... }
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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); }
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 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 UnitsConverter readConverter(Scanner in) { //the code that reads data and returns a UnitsConverter } From a different class, static methods are called using the name of the class instead of the name of an object.