Skip to content

Commit ab9892f

Browse files
authored
BAEL-9485 The Java Object Class (#18883)
* Implement Car class with Object method overrides The Car class implements Cloneable and overrides Object methods for equality, hashing, string representation, and cloning. * Rename Car.java to follow package naming convention * Add package declaration to CarObjectUnitTests * Add package declaration to Car.java * Rename test methods for better readability * Update Car object creation in unit test * Remove synchronization tests for Car class Removed tests for wait(), notify(), and notifyAll() methods due to synchronization issues. * Refactor CarObjectUnitTests for clarity and conciseness Refactored JUnit tests for the Car class by removing comments and simplifying assertions. * Fix formatting in toString() method of Car class
1 parent 13942c0 commit ab9892f

File tree

2 files changed

+170
-0
lines changed
  • core-java-modules/core-java-lang-oop-types/src

2 files changed

+170
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.objectclassguide;
2+
3+
import java.util.Objects;
4+
5+
6+
/**
7+
* Represents a car object, implementing Cloneable and overriding Object methods.
8+
*
9+
* The class uses standard implementation for equals(), hashCode(), and toString().
10+
* The clone() method performs a shallow copy, which is sufficient since 'make' (String)
11+
* and 'year' (int) are immutable or primitive.
12+
*/
13+
public class Car implements Cloneable {
14+
private String make;
15+
private int year;
16+
17+
18+
public Car(String make, int year) {
19+
this.make = make;
20+
this.year = year;
21+
}
22+
23+
24+
// Getters for external access (useful for testing)
25+
public String getMake() {
26+
return make;
27+
}
28+
29+
30+
public int getYear() {
31+
return year;
32+
}
33+
34+
35+
/**
36+
* Standard implementation of equals() for value equality.
37+
*/
38+
@Override
39+
public boolean equals(Object obj) {
40+
if (this == obj) return true;
41+
if (obj == null || getClass() != obj.getClass()) return false;
42+
Car car = (Car) obj;
43+
// Use Objects.equals for safe String comparison
44+
return year == car.year && Objects.equals(make, car.make);
45+
}
46+
47+
48+
/**
49+
* Standard implementation of hashCode() based on make and year.
50+
*/
51+
@Override
52+
public int hashCode() {
53+
return Objects.hash(make, year);
54+
}
55+
56+
57+
/**
58+
* Standard implementation of toString() for debugging and logging.
59+
*/
60+
@Override
61+
public String toString() {
62+
return "Car{"
63+
+ "make='" + make + '\''
64+
+ ", year=" + year
65+
+ '}';
66+
}
67+
68+
69+
/**
70+
* Overrides the protected clone() method from Object to perform a shallow copy.
71+
* This is the standard pattern when implementing the Cloneable marker interface.
72+
*/
73+
@Override
74+
public Object clone() throws CloneNotSupportedException {
75+
// Calls Object's native clone() method
76+
return super.clone();
77+
}
78+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.baeldung.objectclassguide;
2+
3+
import org.junit.jupiter.api.Test;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertNotSame;
8+
9+
public class CarObjectUnitTests {
10+
11+
@Test
12+
void givenACarObject_whenTestingObjectEqualsItself_thenEqualsReflexive() {
13+
Car car1 = new Car("Honda", 2020);
14+
assertTrue(car1.equals(car1));
15+
}
16+
17+
@Test
18+
void givenTwoCarObjects_whenTestingSymmetric_thenEqualsSymmetric() {
19+
Car car1 = new Car("Honda", 2020);
20+
Car car2 = new Car("Honda", 2020);
21+
assertTrue(car1.equals(car2));
22+
assertTrue(car2.equals(car1));
23+
}
24+
25+
@Test
26+
void givenThreeCarObjects_whenTestingTransitive_thenEqualsTransitive() {
27+
Car car1 = new Car("Honda", 2020);
28+
Car car2 = new Car("Honda", 2020);
29+
Car car3 = new Car("Honda", 2020);
30+
31+
assertTrue(car1.equals(car2));
32+
assertTrue(car2.equals(car3));
33+
assertTrue(car1.equals(car3));
34+
}
35+
36+
@Test
37+
void givenTwoDifferentCarObjects_whenComparingWithEquals_thenEqualsReturnsFalse() {
38+
Car car1 = new Car("Honda", 2020);
39+
Car car2 = new Car("Toyota", 2020);
40+
41+
assertFalse(car1.equals(car2));
42+
}
43+
44+
@Test
45+
void givenANonNullCarObject_whenTestingAgainstNull_thenEqualsReturnsFalse() {
46+
Car car1 = new Car("Honda", 2020);
47+
48+
assertFalse(car1.equals(null));
49+
}
50+
51+
@Test
52+
void givenTwoEqualCarObjects_whenComparingHashCodes_thenReturnsEqualHashCodes() {
53+
Car car1 = new Car("Honda", 2020);
54+
Car car2 = new Car("Honda", 2020);
55+
56+
assertEquals(car1.hashCode(), car2.hashCode());
57+
}
58+
59+
@Test
60+
void givenACarObject_whenTestingHashCodeConsistency_thenReturnsSameHashCodeAcrossMultipleCalls() {
61+
Car car = new Car("Honda", 2020);
62+
int initialHash = car.hashCode();
63+
64+
assertEquals(initialHash, car.hashCode());
65+
}
66+
67+
@Test
68+
void givenACarObject_whenTestingToString_thenReturnsExpectedString() {
69+
Car car = new Car("Tesla", 2023);
70+
String expected = "Car{make='Tesla', year=2023}";
71+
72+
assertEquals(expected, car.toString());
73+
}
74+
75+
@Test
76+
void givenACarObject_whenTestingGetClass_thenReturnsCarClass() {
77+
Car car = new Car("Ford", 2015);
78+
79+
assertEquals(Car.class, car.getClass());
80+
}
81+
82+
@Test
83+
void givenACarObject_whenTestingClone_thenCloneSuccess() throws CloneNotSupportedException {
84+
Car original = new Car("Honda", 2020);
85+
Car cloned = (Car) original.clone();
86+
87+
assertNotSame(original, cloned);
88+
assertEquals(original, cloned);
89+
assertEquals(original.getMake(), cloned.getMake());
90+
assertEquals(original.getYear(), cloned.getYear());
91+
}
92+
}

0 commit comments

Comments
 (0)