Skip to content

Commit a19bc60

Browse files
committed
feat: implement Comparable<Employee1> for natural ordering by age
WHAT: - Created `Employee1` class with fields `name` and `age`. - Implemented `Comparable<Employee1>` to define natural ordering logic. - Overrode `compareTo(Employee1 other)` using `Integer.compare()` to compare ages. - Implemented `toString()` for clear object representation. - Added `TestEmployee` class to demonstrate comparison between employees. WHY: - Shows how to use Java’s generic interface `Comparable<T>` to enforce type-safe comparison rules. - Provides a clean, reusable way to sort or prioritize custom objects without external comparator logic. - Demonstrates polymorphism: comparison logic is embedded in the object itself, making sorting collections intuitive. HOW (Logic Flow): 1. `Employee1` objects are created with names and ages. 2. When `compareTo()` is called: - If this.age > other.age → returns positive. - If this.age < other.age → returns negative. - If equal → returns zero. 3. This comparison behavior integrates seamlessly with: - `Collections.sort(List<Employee1>)` - Priority-based data structures like `TreeSet`, `PriorityQueue` - Java Streams `.sorted()` BENEFITS: - Strong compile-time type safety (`Comparable<Employee1>` ensures only Employee1 objects compared). - Simplifies sorting logic in larger applications — no extra comparator needed for default behavior. - Clean and maintainable — toString() makes debugging and logging easier. REAL-WORLD APPLICATIONS: - Sorting employees by age in HR or payroll systems. - Prioritizing users in queue management systems (e.g., oldest first). - Any business domain where objects require a natural ordering rule. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 4b94fe2 commit a19bc60

File tree

2 files changed

+29
-14
lines changed

2 files changed

+29
-14
lines changed
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,40 @@
11
package GenericInterface;
22

3-
/*
4-
Real-World Example of Generic Interfaces
5-
6-
A common example of a generic interface in Java is the Comparable<T> interface, which is used to impose a natural ordering on objects.
7-
*/
8-
9-
/*public class Employee1 implements Comparable<Employee> {
3+
public class Employee1 implements Comparable<Employee1> {
104
private String name;
115
private int age;
126

13-
public Employee(String name, int age) {
7+
public Employee1(String name, int age) {
148
this.name = name;
159
this.age = age;
1610
}
1711

1812
@Override
19-
public int compareTo(Employee other) {
13+
public int compareTo(Employee1 other) {
2014
return Integer.compare(this.age, other.age);
2115
}
22-
}*/
2316

17+
@Override
18+
public String toString() {
19+
return name + " (" + age + ")";
20+
}
21+
}
2422

2523
/*
26-
In this example, the Comparable<Employee> interface ensures that the Employee objects can be compared based on their age.
27-
Summary
24+
Real-World Example of Generic Interfaces:
25+
A common example of a generic interface in Java is the Comparable<T> interface, which is used to impose a
26+
natural ordering on objects.
27+
28+
In this example, the Comparable<Employee> interface ensures that the Employee objects can be compared based on
29+
their age.
30+
31+
Summary:
2832
A generic interface allows you to write interfaces that can handle various types specified at the time of implementation.
2933
Type parameters are used to define the type of objects that the interface will work with.
3034
You can implement a generic interface by specifying the type or by keeping the implementation generic.
3135
Multiple type parameters can be used to work with more than one type at a time.
3236
Bounded type parameters restrict the types that can be passed to the generic interface.
3337
Wildcards can be used in generic interfaces for flexibility in accepting various types.
3438
Generic interfaces in Java are powerful tools that increase the reusability and flexibility of your code by
35-
allowing you to abstract behaviour across multiple types, while maintaining type safety at compile time.
36-
*/
39+
allowing you to abstract behavior across multiple types, while maintaining type safety at compile time.
40+
*/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package GenericInterface;
2+
3+
public class TestEmployee {
4+
public static void main(String[] args) {
5+
Employee1 e1 = new Employee1("Alice", 30);
6+
Employee1 e2 = new Employee1("Bob", 25);
7+
8+
int result = e1.compareTo(e2);
9+
System.out.println(result); // >0 means e1 is older, <0 means e2 is older
10+
}
11+
}

0 commit comments

Comments
 (0)