ADVERTISEMENT
Google AdSense Slot (headerLeaderboard)
Set your Publisher ID in src/config/ads.ts to display live ads.
Java Tutorial• 8 min read
The 4 OOP Pillars: Encapsulation, Inheritance, Polymorphism, Abstraction
private getters/setters, extends, super, @Override, abstract classes, and interfaces
Object-Oriented Programming in Java is anchored on four fundamental pillars that promote modularity, reuse, and maintainability.
The 4 Pillars in Detail
1Encapsulation: Keeping fields
private and exposing access via public getters/setters with validation.2Inheritance: Subclasses inherit state and behavior from a superclass using
extends.3Polymorphism: Objects can be treated as instances of their parent type, invoking overridden methods dynamically at runtime (Dynamic Method Dispatch).
4Abstraction: Hiding internal implementation details using
abstract class and interface.Polymorphism and Interfaces in Action
interface PaymentGateway {
void processPayment(double amount);
}
class StripeGateway implements PaymentGateway {
@Override
public void processPayment(double amount) {
System.out.println("Processing $" + amount + " via Stripe API.");
}
}
class PayPalGateway implements PaymentGateway {
@Override
public void processPayment(double amount) {
System.out.println("Processing $" + amount + " via PayPal Wallet.");
}
}
public class Checkout {
public static void executeOrder(PaymentGateway gateway, double total) {
gateway.processPayment(total); // Polymorphic invocation!
}
}ADVERTISEMENT
Google AdSense Slot (inContentBanner)
Set your Publisher ID in src/config/ads.ts to display live ads.
Test Yourself With Exercise
Which keyword in Java allows a class to implement a contract defined by an interface?