Core Java OOPs: Enterprise Implementation
Java is strictly object-oriented (mostly). In large systems, we use OOPs not just for organization, but for Decoupling and Polymorphism.
Inheritance and Interfaces
In Java, a class can only extend one superclass (Single Inheritance) but can implement multiple interfaces.
- Abstract Class: Use when you want to share code among several closely related classes.
- Interface: Use to define a contract. Modern Java (8+) allows
defaultandstaticmethods in interfaces.
java codepublic interface PaymentGateway { void process(double amount); default void logTransaction(String id) { System.out.println("Logging: " + id); } }
Polymorphism: Static vs Dynamic
- Static (Method Overloading): Handled at compile time. Same method name, different parameters.
- Dynamic (Method Overriding): Handled at runtime. Subclass provides a specific implementation of a method already defined in its parent.
Encapsulation & Records
Encapsulation (using private fields and public getters/setters) protects the internal state.
In Java 14+, use records for immutable data carriers.
java code// Modern Java Record public record User(String id, String email) {}
Composition over Inheritance
Industry experts prefer composition (having an instance of another class) over inheritance. It's more flexible and avoids the "Fragile Base Class" problem.