Presentation is loading. Please wait.

Presentation is loading. Please wait.

Handout-4b More on Classes

Similar presentations


Presentation on theme: "Handout-4b More on Classes"— Presentation transcript:

1 Handout-4b More on Classes
Overview Overloading The this keyword The toString method The equals method

2 Overloading Java allows a class to have more than one constructor.
Of course such constructors must have the same name – name of the class. This is called overloading. 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 Overloading (cont’d) 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(); 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) public Point max(Point a, Point b) { if (a.distanceToOrigin() > b.distanceToOrigin())

4 Overloading (cont’d) An important point to note is that overloaded methods and constructors must have different signatures. That is, the formal parameters of overloaded constructors and methods must not have the same type, order, and count all at the same time. The return type of a method is not counted as part of its signature. The following shows an example of illegal overloading. public double compute(int num) { . . . } public int compute(int num) {

5 The this keyword The this keyword is used as a reference to the current object. It has dual usage: It can be used with a dot to refer to the fields and methods of this object. Example. this.x means the x variable of the current object. 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. The this keyword can also be used to call another constructor of a class from another constructor of the same class. 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 The toString method It is very common to print the content of an object. Thus, when designing a class, a toString method that returns the string representation of the object is usually provided. 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. 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 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. It is thus, very useful to always provide a toString method for each class we define .

7 The equals method Sometimes we would like to compare between contents of two objects Since the == operator only compares references of two objects, it is usually necessary to provide an equals method for a class. If an equals method is not provided, the equals method of the standard Object class is used. However, this is useless because it compares only references. Thus, again it is important that we implement an equals method for each class we design. 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 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);


Download ppt "Handout-4b More on Classes"

Similar presentations


Ads by Google