Download presentation
Presentation is loading. Please wait.
1
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method
2
2 Overloading l Java allows a class to have more than one constructor. l Of course such constructors must have the same name – name of the class. This is called overloading. l For example, we could define another constructor for the Point class as follows: public class Point { private int x; private int y; public Point (int x1, int y1) { x = x1; y = y1; } public Point() ( x=0; y=0; }.... }
3
3 Overloading (cont’d) l Having multiple constructors gives the user a choice when constructing an object of the class. Point p1 = new Point(10, 20); Point p2 = new Point(); l Methods can also be overloaded. For example, all the following methods can be defined in one class. public double max(double a, double b) { if (a >= b) return a; else return b; } public String max(String a, String b) { if (a.compareTo(b) > 0) return a; else return b; } public Point max(Point a, Point b) { if (a.distanceToOrigin() > b.distanceToOrigin()) return a; else return b; }
4
4 Overloading (cont’d) l An important point to note is that overloaded methods and constructors must have different signatures. l That is, the formal parameters of overloaded constructors and methods must not have the same type, order, and count all at the same time. l The return type of a method is not counted as part of its signature. l The following shows an example of illegal overloading. public double compute(int num) {... } public int compute(int num) {... }
5
5 The this keyword l The this keyword is used as a reference to the current object. It has dual usage: l It can be used with a dot to refer to the fields and methods of this object. l Example. this.x means the x variable of the current object. l This method is often used to resolve scope conflict between the parameters of a method or constructor and an instance variable with the same name. l The this keyword can also be used to call another constructor of a class from another constructor of the same class. l The following shows both uses of the this keyword. class Point { private double x; private double y; public Point(double x, double y) { this.x=x; this.y=y; } public Point() { this(0.0, 0.0); }..... }
6
6 The toString method l It is very common to print the content of an object. l Thus, when designing a class, a toString method that returns the string representation of the object is usually provided. l If the toString method is provided for a class, Java automatically calls it whenever an object of the class is used where a string is expected. Example in a printing or concatenation operations. l If a toString method is not provided, the toString method of the standard class Object is used – This is not very useful as it returns a string in the form ClassName@memoryAddress Where ClassName is the name of the class from which the object is created and memory address is the location of the object in the memory. l It is thus, very useful to always provide a toString method for each class we define.
7
7 The equals method l Sometimes we would like to compare between contents of two objects l Since the == operator only compares references of two objects, it is usually necessary to provide an equals method for a class. l If an equals method is not provided, the equals method of the standard Object class is used. l However, this is useless because it compares only references. l Thus, again it is important that we implement an equals method for each class we design. l The next example modifies the Point class by providing the equals and toString methods. public class Point { private double x; private double y; public Point(double x, double y) { this.x=x; this.y=y; } public Point() { this(0.0, 0.0); }
8
8 The equals method (cont’d) public double distanceToOrigin() { return Math.sqrt((x * x) + (y * y)); } public double distanceToPoint(Point p) { return Math.sqrt(((x - p.x) * (x - p.x)) + ((y - p.y) * (y - p.y))); } public void moveTo(double x, double y) { this.x=x; this.y=y; } public void translate(double dx, double dy) { x += dx; y += dy; } public double getX() { return x; } public double getY() { return y; } public String toString() { return "("+x+", "+y+")"; } public boolean equals(Point p) { return (this.x == p.x && this.y == p.y); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.