Friday, 23 August 2013

How to print the address of an object if you have redefined toString method

How to print the address of an object if you have redefined toString method

I'm a newbie to Java. Now I'm studying equals and == and redefinition of
equals and toString.
I would like to use both the toString method that I have redefied and the
default method that is inherited from the Object class.
I failed to use that super modificator to reach that method.
This is for educational purposes only. What I would like to get is more
clear if you will have a look at the comments in my code.
Could you help me here?
My code is:
public class EqualTest{
public static void main(String[] args){
Employee alice1 = new Employee("Alice Adams", 75000, 1987, 12, 15);
//System.out.super.println(alice1);
Employee alice2 = alice1;
//System.out.super.println(alice2);
Employee alice3 = new Employee("Alice Adams", 75000, 1987, 12, 15);
//System.out.super.println(alice3);
System.out.println("alice1==alice2: " + (alice1==alice2));
System.out.println("alice1 == alice3: " + (alice1==alice3));
System.out.println("alice1.equals(alice3): " +
alice1.equals(alice3));
}
}
class Employee{
...
public String toString(){
return getClass().getName() + "[name = " + name +
", salary=" + salary + ", hireDay=" + hireDay + "]";
}
}

No comments:

Post a Comment