Implementing Tostring Method in Java Is Done by Overriding the Object S Tostring Method

Implementing Tostring Method in Java Is Done by Overriding the Object S Tostring Method

ALIGARH MUSLIM UNIVERSITY

Department of Computer Science

Course: MCA CSM-2201: Object Oriented Programming Using JAVA

Academic Session 2017-2018

Handout-4 (c)

Topic: More Use of toString( ) Method

Dr. Rafiqul Zaman Khan, Professor (Computer Science).

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Objective:

  • To learn more about toString( ) method.

Implementing toString method in java is done by overriding the Object’s toString method. The java toString() method is used when we need a string representation of an object. It is defined in Object class. This method can be overridden to customize the String representation of the Object.

Example#1:

Below is a program showing the use of the Object’s Default toString java method.

class PointCoordinates

{

private int x, y;

public PointCoordinates(int x, int y)

{

this.x = x;

this.y = y;

}

public int getX()

{

return x;

}

public int getY()

{

return y;

}

}

public class ToStringDemo

{

public static void main(String args[])

{

PointCoordinates point = new PointCoordinates(10, 10);

// using the Default Object.toString() Method

System.out.println("Object toString() method : "+point);

// implicitly call toString() on object as part of string concatenation

String s = point + " testing";

System.out.println(s);

}

}

Sample output:

Object toString() method : PointCoordinates@111f71

PointCoordinates@111f71 testing

In the above example when we try printing PointCoordinates object, it internally calls the Object’s toString() method as we have not overridden the java toString() method. Since out example has no toString method, the default one in Object is used. The format of the default toString method of the Object is as shown below.

Class Name, “@”, and the hex version of the object’s hashcode concatenated into a string.
The default hashCode method in Object is typically implemented by converting the memory address of the object into an integer.

Below is an example shown of the same program by Overriding the default Object toString() method. The toString() method must be descriptive and should generally cover all the contents of the object.

class PointCoordinates

{

private int x, y;

public PointCoordinates(int x, int y)

{

this.x = x;

this.y = y;

}

public int getX()

{

return x;

}

public int getY()

{

return y;

}

//Custom toString() Method.

public String toString()

{

return "X=" + x + " " + "Y=" + y;

}

}

public class ToStringDemo2

{

public static void main(String args[])

{

PointCoordinates point = new PointCoordinates(10, 10);

// using the Overridden Object.toString() Method

System.out.println(point);

String s = point + " testing";

System.out.println(s);

}

}

Here toString() method is used to returns the string representation of your object. It is very easy process to do this. You can use toString() method any where in the program.

Example#2:

In the following program I have use PenClass to include all methods.

public class PenClass

{

int x, y;

public PenClass(int x, int y)

{

this.x = x;

this.y = y;

}

public String toString()

{

String rets = x+" "+y+"\n";

return rets;

}

public static void main(String[] args)

{

PenClass x = new PenClass(1,2), y = new PenClass(3,4);

System.out.println(x+""+y);

}

}

Sample output:

1 2

3 4

Here toString method can be implemented in java by overriding the Object’s toString method. When we want string representation of an object we have to use java toString() method. It is very easy to use.

Example#3:

Just try to understand following example.

class PointCoordinates

{

private int a, b;

public PointCoordinates(int a, int b)

{

this.a = a;

this.b = b;

}

public int geta()

{

return a;

}

public int getb()

{

return b;

}

public String toString()

{

return "a=" + a + " " + "b=" + b;

}

}

public class ToStringDemo

{

public static void main(String args[])

{

PointCoordinates point = new PointCoordinates(20, 20);

System.out.println(point);

String s1 = point + " tes1ting";

System.out.println(s1);

}

}

Sample output:

a=20 b=20

a=20 b=20 tes1ting

Example#4:

Just try to understand following example. ToString() is used for Converting Numbers to Strings. It is very easy to use toString() method. In the following example I have use ToStringeg class to include all methods. I have use ToString() to convert Numbers to Strings.

class ToString

{

int value;

ToString()

{

value = 0;

}

ToString(int its)

{

value = its;

}

public String toString()

{

return "-->" + value + "<--";

}

}

public class Demo

{

public static void main(String args[])

{

ToString s=new ToString(10);

System.out.println("Hello, World of Javas");

System.out.println("My object is" + s);

}

}

Sample output:

Hello, World of Javas

My object is-->10<--

Example#5:

public class PencilClass

{

int x, y;

public PencilClass(int x, int y)

{

this.x = x;

this.y = y;

}

public String toString()

{

String rets = x+" "+y+ " getX= "+getX()+ " getY= "+getY()+"\n";

return rets;

}

public int getX()

{

return x;

}

public int getY()

{

return y;

}

public static void main(String[] args)

{

PencilClass x = new PencilClass(1,2), y = new PencilClass(3,4);

System.out.println(x+""+y);

}

}

Sample output:

1 2 getX= 1 getY= 2

3 4 getX= 3 getY= 4

Example#6:

class Gs

{

private double x, y,z;

public Gs(double x, double y,double z)

{

this.x = x;

this.y = y;

this.z= z;

}

public Gs(double x, double y)

{

this(x,y,0.0);

}

public Gs(double x)

{

this(x,0.0,0.0);

}

public double getRA()

{

return 0.5*x*y;

}

public double getCA()

{

return Math.PI*x*x;

}

}

public class p

{

public static void main(String[] args)

{

Gs c1=new Gs(1.0);

Gs r1=new Gs(1.0,2.0);

System.out.println("ca= "+c1.getCA());

System.out.println("ra= "+r1.getRA());

}

}

Sample output:

ca= 3.141592653589793

ra= 1.0

Example#7:

import java.io.*;

class Employee

{

private int id;

private String name;

private double salary;

public Employee(int id, String name, double salary) // constructor

{

this.id = id;

this.name = name;

this.salary = salary;

}

public Employee(int id, String name) // constructor

{

this(id, name, 0.0);

}

public void setSalary(double salary) // method

{

this.salary = salary;

}

public int getId() //method

{

return id;

}

public String getName() // method

{

return name;

}

public double getSalary() // method

{

return salary;

}

public void deductions(double telephoneBills) // method

{

salary -= telephoneBills;

}

public void deductions(double telephoneBills, double medicalBills) // method

{

salary -= (telephoneBills + medicalBills);

}

public void raiseSalary(double percentIncrease) // method

{

salary += salary * percentIncrease/100;

}

public String toString() // method

{

return "\nID#: " + id + "\nName: " + name+"\nSalary: "+salary;

}

}// end of class

public class Demo

{

public static void main(String[] args) throws IOException

{

BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

int id;

String name;

double salary;

System.out.print("Enter Name for Employee 1: ");

name = stdin.readLine();

System.out.print("Enter ID Number for Employee 1: ");

id = Integer.parseInt(stdin.readLine());

System.out.print("Enter Salary for Employee 1: ");

salary = Double.parseDouble(stdin.readLine());

Employee emp1 = new Employee (id, name, salary);

System.out.print("\nEnter Name for Employee 2: ");

name = stdin.readLine();

System.out.print("Enter ID Number for Employee 2: ");

id = Integer.parseInt(stdin.readLine());

Employee emp2 = new Employee (id, name);

emp2.setSalary(emp1.getSalary());

emp1.deductions(50);

emp2.deductions(60, 40);

System.out.println(emp1);

System.out.println(emp2);

} // end of main method

} // end of class

Sample output:

Enter Name for Employee 1: Khan

Enter ID Number for Employee 1: 786

Enter Salary for Employee 1: 70000

Enter Name for Employee 2: Suhail Ahmad Khan

Enter ID Number for Employee 2: 100000

ID#: 786

Name: Khan

Salary: 69950.0

ID#: 100000

Name: Suhail Ahmad Khan

Salary: 69900.0

1