You are on page 1of 11

Class Object

Christiane, Ralf, Ann, Tom

Hasso Plattner Institute


Recap: Object Data Types

■  Every class may serve as an (object-) data type


□  Example: String à Object data type from the Java API
□  https://docs.oracle.com/javase/8/docs/api/
■  Detective is an object data type that was declared by us

Detective duke = new Detective(); Detective

Class / Reference / Class


Object
Data Type Identifier Constructor

2
Get a String from our Detective

1 Detective duke = new Detective();


2
3 /* throws Exception */
4 String myString = (String) duke; Error
5
7 System.out.println(myString);

Exception in thread "main" java.lang.Error:


Unresolved compilation problem:
Cannot cast from Detective to String

3
equals and toString

■  Each class inherits these two methods from Object

□  toString()
–  Returns a String representation of the object
–  Default: ClassName@HashCode, e.g.: Detective@70dea4e
–  Return value should be individualized to the respective class

4
toString Code Example

1 public class Detective /* extends Object */ {


2 private String name;
3 private int age;
4
5 [...]
6
7 @Override
8 public String toString() {
9 String output = "Detective " + this.name;
10 output += " is " + this.age + " years old.";
11 return output;
12 }
13 }
14
15
5
toString in Action

1 Detective detective1 = new Detective("Duke", 42);


2 Detective detective2 = new Detective("Dennis", 33);
3
4 System.out.println(detective1.toString());
5 System.out.println(detective2);

Detective Duke is 42 years old.


Detective Dennis is 33 years old.

6
equals and toString

■  Each class inherits these two methods from Object

□  toString()
–  Returns a String representation of the object
–  Default: ClassName@HashCode
–  Return value should be individualized to the respective class

□  equals(Object obj)
–  Compares two objects
–  Default: Compares the memory addresses of the objects
–  Should be overriden for own classes to test specific attributes

7
equals Code Example

1 public class Detective /* extends Object */ {


2 private String name;
3 private int age;
4
5 @Override
6 public boolean equals(Object o) {
7 return false; // or true
8
9
10
11 }
12
13 [...]
14
15 }
8
equals Code Example

1 public class Detective /* extends Object */ {


2 private String name;
3 private int age;
4
5 @Override
6 public boolean equals(Object o) {
Short-Circuit Evaluation
7 boolean result = false;
8 if ((o != null) && (o instanceof Detective)){
9 Detective detective2 = (Detective) o;
10 result = this.name.equals(detective2.name) &&
11 this.age == detective2.age;
12 }
13 return result;
14 }
15 [...]
16 }
9
equals in Action

1 Detective detective1 = new Detective("Duke", 42);


2 Detective detective2 = new Detective("Dennis", 33);
3 Object detective3 = new Detective("Duke", 42);
4
5 System.out.println(detective1.equals(detective2));
6 System.out.println(detective1.equals(detective3));

false
true

10
Class Object
Christiane, Ralf, Ann, Tom

Hasso Plattner Institute