class PrintObject {
public static void main(String[] args) {
Rectangle r = new Rectangle(10, 20);
Container c = new Container(100, 200, 300);
System.out.println(r);/*Classname@hashCode*/
System.out.println(r.toString());/*Classname@hashCode*/
System.out.println(r.hashCode());/*hash code in integer form*/
System.out.println(Integer.toHexString(
r.hashCode()));/*hash code in hexadecimal form*/
System.out.println(r.getClass().getName());/*returns classname*/
System.out.println(r);
System.out.println(r.getClass().getName() +
"@" +
Integer.toHexString(r.hashCode()));/*mimicking the operation done while an object is printed*/
System.out.println(r);
System.out.println(c);
}
}
java.lang.Object.toString(); //programmer need to override it to personalize it
java.lang.Object.hashCode();//programmer need to override it to personalize it
java.lang.Object.getClass();//no overriding
java.lang.Object.equals();//programmer need to override it to personalize it
---Overriding toString() in class Rectangle---
public String toString() {
return "Rectangle[length=" + length +
", breadth=" + breadth + "]";
}
---Overriding toString() in class Container---
public String toString() {
return "Container[length=" + getLength() +
", breadth=" + getBreadth() +
", height=" + height + "]";
}
/*length and breadth are private in Rectangle and are not inherited to Container. So getLength() and getBreadth() are used in Container*/
0 Comments