Advanced Class Techniques
Generics in Class Design
Generic Class Implementation
public class GenericStorage<T> {
private T[] elements;
private int size;
@SuppressWarnings("unchecked")
public GenericStorage(int capacity) {
elements = (T[]) new Object[capacity];
size = 0;
}
public void add(T element) {
if (size < elements.length) {
elements[size++] = element;
}
}
public T get(int index) {
if (index >= 0 && index < size) {
return elements[index];
}
throw new IndexOutOfBoundsException("Invalid index");
}
}
Nested and Inner Classes
Types of Inner Classes
Inner Class Type |
Description |
Use Case |
Member Inner Class |
Defined within another class |
Encapsulating helper functionality |
Static Nested Class |
Declared as static |
Utility classes |
Local Inner Class |
Defined inside a method |
Complex method-specific logic |
Anonymous Inner Class |
No explicit class definition |
Event handling, callback implementations |
Anonymous Inner Class Example
public interface Runnable {
void execute();
}
public class TaskExecutor {
public void runTask(Runnable task) {
task.execute();
}
public static void main(String[] args) {
TaskExecutor executor = new TaskExecutor();
// Anonymous inner class
executor.runTask(new Runnable() {
@Override
public void execute() {
System.out.println("Task executed!");
}
});
}
}
Class Introspection
public class ReflectionDemo {
public void inspectClass(Class<?> clazz) {
System.out.println("Class Name: " + clazz.getName());
// Print declared methods
System.out.println("Declared Methods:");
for (Method method : clazz.getDeclaredMethods()) {
System.out.println(method.getName());
}
// Print constructors
System.out.println("Constructors:");
for (Constructor<?> constructor : clazz.getConstructors()) {
System.out.println(constructor);
}
}
}
Design Patterns
Singleton Pattern
public class DatabaseConnection {
private static DatabaseConnection instance;
private DatabaseConnection() {
// Private constructor to prevent direct instantiation
}
public static synchronized DatabaseConnection getInstance() {
if (instance == null) {
instance = new DatabaseConnection();
}
return instance;
}
public void connect() {
System.out.println("Database connected");
}
}
Class Relationship Visualization
classDiagram
class AbstractFactory {
+createProduct()
}
class ConcreteFactoryA {
+createProduct()
}
class ConcreteFactoryB {
+createProduct()
}
class Product {
+operation()
}
AbstractFactory <|-- ConcreteFactoryA
AbstractFactory <|-- ConcreteFactoryB
Product <.. AbstractFactory
Advanced Inheritance Techniques
Interface Default Methods
public interface Loggable {
default void log(String message) {
System.out.println("[LOG] " + message);
}
void performAction();
}
public class LoggableService implements Loggable {
@Override
public void performAction() {
log("Action performed");
// Actual implementation
}
}
Immutable and Flyweight Patterns
public final class ImmutablePoint {
private final int x;
private final int y;
public ImmutablePoint(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() { return x; }
public int getY() { return y; }
}
LabEx Best Practices
Key recommendations for advanced class techniques:
- Use generics for type-safe collections
- Leverage inner classes for complex logic
- Implement design patterns judiciously
- Prioritize immutability and thread safety
- Use reflection sparingly