diff --git a/05-generics/snippets/README.md b/05-generics/snippets/README.md new file mode 100644 index 00000000..31c90b58 --- /dev/null +++ b/05-generics/snippets/README.md @@ -0,0 +1 @@ +# Generics / Code snippets diff --git a/05-generics/snippets/src/BridgeMethods.java b/05-generics/snippets/src/BridgeMethods.java new file mode 100644 index 00000000..974e34b7 --- /dev/null +++ b/05-generics/snippets/src/BridgeMethods.java @@ -0,0 +1,24 @@ +class Box { + private T value; + + public void setValue(T value) { + this.value = value; + } +} + +class BoxOfInt extends Box { + private Integer value; + + public void setValue(Integer value) { + this.value = value; + } +} + +public class BridgeMethods { + + public static void main(String... args) { + Box boxOfInt = new BoxOfInt(); + boxOfInt.setValue(1); + } + +} diff --git a/05-generics/snippets/src/BridgeMethodsExample.java b/05-generics/snippets/src/BridgeMethodsExample.java deleted file mode 100644 index 25d258da..00000000 --- a/05-generics/snippets/src/BridgeMethodsExample.java +++ /dev/null @@ -1,85 +0,0 @@ -import java.util.HashMap; -import java.util.Map; - -// Base class Entity -abstract class Entity { - private final long id; - - public Entity(long id) { - this.id = id; - } - - public long getId() { - return id; - } -} - -// Generic Storage class for Entity -class EntityStorage { - private Map entities; - - public EntityStorage() { - this.entities = new HashMap<>(); - } - - public T getEntity(long id) { - return entities.get(id); - } - - public void addEntity(T entity) { - this.entities.put(entity.getId(), entity); - } -} - -// Specialized Storage class for Employee -class Employee extends Entity { - private String name; - - public Employee(long id, String name) { - super(id); - this.name = name; - } - - public String getName() { - return name; - } -} - -// Specialized Storage class for EmployeeStorage with bridge method -class EmployeeStorage extends EntityStorage { - public EmployeeStorage() { - super(); - } - - // Additional methods specific to EmployeeStorage - public String getEmployeeName(long id) { - return getEntity(id).getName(); - } - - // Due to type erasure, the getEntity method in the EmployeeStorage class has an erased signature. - // The compiler generates a bridge method in the EmployeeStorage class to ensure that the - // overridden method in the EntityStorage interface adheres to the erasure rules. - // - // Bridge methods are synthetic methods added by the compiler to maintain polymorphism - // and type safety in the presence of type erasure. They are not directly written - // in the source code but are introduced during the compilation process. - @Override - public Employee getEntity(long id) { - return super.getEntity(id); - } -} - -public class BridgeMethodsExample { - public static void main(String[] args) { - Employee employee = new Employee(1, "John Doe"); - - EmployeeStorage employeeStorage = new EmployeeStorage(); - - // Access specific method - System.out.println("Employee Name: " + employeeStorage.getEmployeeName(1)); - - // Access generic method through the bridge - Entity genericEntity = employeeStorage.getEntity(1); - System.out.println("Generic Entity ID: " + genericEntity.getId()); - } -}