Java Oops
Core Pillars
On this page
Inheritance: Reusing Code
01
Notes
Inheritance lets a child class acquire properties and behavior from a parent class.
It eliminates code duplication across similar features.
Example: Blinkit IS-A DeliveryApp.
Blinkit automatically gets core features like login() and trackOrder() from DeliveryApp.
The extends & super Keywords
02
Notes
extends connects child class to parent class (e.g., class Blinkit extends DeliveryApp).
super refers directly to the immediate parent class.
Use super() to call the parent's constructor before adding child-specific features.
Important: super() must always be the very first statement in a constructor.
Method Overriding Basics
03
Notes
Occurs when a child class rewrites a parent method to give its own implementation.
Parent DeliveryApp defines calculateFee().
Zepto overrides calculateFee() to offer free delivery on orders above ₹199.
Java executes the method based on the actual object created.
Polymorphism: Many Forms
04
Notes
Polymorphism means 'one name, multiple behaviors'.
Allows a single interface to handle different underlying operations.
Example: The 'Pay' button on Swiggy behaves differently for UPI, Credit Card, or Paytm.
Two main types: Compile-Time (Overloading) and Runtime (Overriding).
Compile-Time Polymorphism (Overloading)
05
Notes
Multiple methods in the same class share the exact same name.
They MUST have different parameters (count, type, or sequence).
Example: Flipkart search("IPhone") vs search("Mobiles", 50000).
The compiler decides which method to run based on passed arguments.
Runtime Polymorphism (Overriding)
06
Notes
A parent reference points to a child object at runtime.
Example: Payment p = new PhonePe();
Both parent and child share the same method signature pay().
Java checks the actual object at runtime and triggers PhonePe's UPI PIN screen.
Overloading vs Overriding
07
Notes
Overloading: Same class, same method name, DIFFERENT parameters (Compile-Time).
Overriding: Parent-Child classes, same method name, SAME parameters (Runtime).
Overloading does NOT require inheritance; Overriding REQUIRES inheritance.
Overloading changes inputs; Overriding replaces logic completely.
Abstraction: Hiding Details
08
Notes
Abstraction exposes WHAT an object does while hiding HOW it works internally.
Keeps user interface simple and hides background complexity.
Example: Booking a ticket on IRCTC app with a single click.
You don't see backend database locks, seat allocation logic, or railway servers.
Abstract Class vs Interface
09
Notes
Abstract Class (extends): Best for related classes sharing state/code (e.g., base Cab for Ola/Uber).
Interface (implements): A strict contract for capabilities (e.g., GPSLocatable, Refundable).
A class can extend only ONE class, but can implement MULTIPLE interfaces.
Interfaces cannot hold instance states; Abstract classes can.
Composition vs Inheritance
10
Notes
Inheritance models an 'IS-A' relationship (Rapido IS-A TransportService).
Composition models a 'HAS-A' relationship (ZomatoOrder HAS-A DeliveryPartner).
Composition joins independent objects together, making code flexible and loosely coupled.
Design Rule: Prefer Composition over Inheritance for easier changes later.