OOP Fundamentals
Objects, Classes, Four Pillars of OOP, Relationships, Interview Prep
- Understand what OOP is and why it exists
- Differentiate between Procedural and OOP paradigms
- Master the four pillars: Abstraction, Encapsulation, Inheritance, Polymorphism
- Understand OOP relationships: IS-A, HAS-A, Aggregation, Composition
All Visual Image Notes
40 SlidesSlide 1 of 40 — OOP Full Mind Ma

Series:
Java Core Concepts | Chapter: OOP Fundamentals Audience: Beginner to Intermediate Java Developers Coverage: Objects, Classes, Four Pillars of OOP, Relationships, Interview Prep
Table of Contents
1. What is OOP?
Overview
OOP stands for Object-Oriented Programming. As the name suggests, it is a programming paradigm that orients all software design around objects. Whenever you design a system, the first question you should ask is: What are the objects present in this problem?
Everything revolves around objects — their properties, their behaviors, and how they interact with each other.
Real-world Analogy
Think of any real-world thing: a car, a dog, an ATM machine. Each of these is an entity you can observe. Each has:
- Things you can describe just by looking at it (e.g., a car's color, brand, type)
- Things it can do (e.g., a car can accelerate, brake, honk)
These two aspects — what it is and what it does — are the foundation of every object in OOP.
Every Object Has Two Things
| Aspect | OOP Term | Also Called | Example (Dog) |
|---|---|---|---|
| Descriptive characteristics | Properties | State / Data Members / Fields | color, breed, age |
| Actions it can perform | Behavior | Methods / Functions / Data Methods | bark(), sleep(), eat() |
Mind Map OOP Overview
2. Procedural vs Object-Oriented Programming
What is Procedural Programming?
Before OOP existed, developers used procedural programming (C is a classic example). In this style:
- A program is broken into functions (also called procedures)
- Functions are called one after another, passing data between them
- Data moves freely from function to function
Visualizing Procedural Flow
1Task → function1(data) → function2(data) → function3(data) → Result2 ↑ ↑3 can change data can change dataAny function in the chain can read and modify shared data. The creator of the data has no control over what others do with it.
Problems with Procedural Programming
| Problem | Explanation |
|---|---|
| No data hiding | Data moves freely; any function can modify it |
| Tight coupling | Functions are highly dependent on each other's data |
| No code reusability | Logic is hard to reuse across different programs |
| Priority on functions | Focus is on what to do, not what data to protect |
| No inheritance | Cannot build on existing code structures cleanly |
How OOP Solves This
OOP binds data and functions together inside a unit called a class. This means:
- Data does not move freely
- Only the class's own methods can directly modify its data
- Other classes must go through controlled access (via methods)
- This brings loose coupling — classes can change internally without breaking others
| Feature | Procedural | OOP |
|---|---|---|
| Priority | Functions | Data |
| Data movement | Freely passed | Controlled via methods |
| Coupling | Tight | Loose |
| Reusability | Difficult | Achieved via Inheritance |
| Data hiding | ❌ | ✅ via Encapsulation |
| Overloading | ❌ | ✅ |
| Inheritance | ❌ | ✅ |
3. Objects and Classes
What is an Object?
Definition
An object is a real-world entity that has both properties (state) and behavior (functionality). In Java, an object is a concrete instance of a class that lives in memory (heap).
Identifying an Object
Ask two questions:
- Can I describe it by just observing it? → Those are its properties
- Does it do something / perform actions? → Those are its behaviors
If yes to both → it qualifies as an object.
Example 1 Dog
| Component | Values |
|---|---|
| Properties | color: black, breed: Labrador, age: 3 |
| Behaviors | bark(), sleep(), eat() |
Example 2 Car
| Component | Values |
|---|---|
| Properties | brand: Toyota, type: SUV, color: white |
| Behaviors | applyBrake(), drive(), increaseSpeed(), honk() |
What is a Class?
Definition
A class is a blueprint or template or skeleton from which objects are created. It defines what properties and behaviors an object of that type will have — but it doesn't hold actual data itself. The object holds the actual data.
Analogy:
A class is like an architectural blueprint of a house. The blueprint describes the rooms, doors, windows — but it is not a house itself. You build actual houses (objects) from the blueprint (class).
What a Class Contains
1Class2├── Data Variables (Properties / State / Fields)3│ └── int age, String address, String id4└── Data Methods (Behavior / Functions)5 └── getAge(), setAge(), updateAddress()Syntax Defining a Class in Java
1class Student {2 // --- Data Variables (Properties) ---3 int age;4 String address;5 String id;6
7 // --- Data Methods (Behaviors) ---8 void updateAddress(String newAddress) {9 this.address = newAddress;10 }11
12 int getAge() {13 return age;14 }15
16 void setAge(int age) {17 this.age = age;18 }19}Syntax Breakdown
| Keyword/Symbol | Meaning |
|---|---|
class | Declares a class |
Student | Name of the class (PascalCase convention) |
int age | Data variable (field) storing an integer |
String address | Data variable storing text |
void updateAddress(...) | Data method that modifies a field |
int getAge() | Data method that returns the age field |
this.address | Refers to the current object's field (covered in depth later) |
Creating Objects from a Class
A class alone does nothing — you must instantiate it (create an object) using the new keyword.
1// Syntax: ClassName variableName = new ClassName();2
3Student engineeringStudent = new Student();4Student mbaStudent = new Student();5Student csStudent = new Student();Each object created is independent. Setting age on engineeringStudent does not affect mbaStudent.
Object in Memory
1Heap Memory2┌─────────────────────────────────┐3│ engineeringStudent │4│ ├── age = 23 │5│ ├── address = "Jaipur" │6│ └── methods: getAge, setAge... │7├─────────────────────────────────┤8│ mbaStudent │9│ ├── age = 25 │10│ ├── address = "Delhi" │11│ └── methods: getAge, setAge... │12└─────────────────────────────────┘Class vs Object Summary Table
| Aspect | Class | Object |
|---|---|---|
| What it is | Blueprint / Template | Real instance in memory |
| Where it lives | Method Area (JVM) | Heap Memory |
| How many | One class definition | Can create unlimited objects |
| Holds data? | No — defines structure | Yes — holds actual values |
| Created using | class keyword | new keyword |
Diagram Class to Objects
4. The Four Pillars of OOP
Pillar 1 Data Abstraction
Overview
Abstraction means hiding the internal implementation details and exposing only what is necessary to the user. The user interacts with a simplified interface without knowing how things work internally.
Why This Concept Exists
- Users don't need to know how something works — only that it works
- Prevents information overload (client code becomes simpler)
- Protects internal implementation from external interference
- Provides security and confidentiality
Real-world Analogies
Example 1 — Car Brake Pedal: When you press the brake pedal, the car slows down. But do you know what happens internally? Engine braking, hydraulic pressure, ABS kick-in, friction pads? No — and you don't need to. The essential feature exposed to you is: "Press this → car slows down." Everything else is abstracted.
Example 2 — Mobile Phone Call: You dial a number and press the green button → call connects. You don't know how the signal is encoded, transmitted via towers, routed through networks, and decoded at the receiver. All of that is hidden. You see only the essential feature.
Definition
Abstraction
hides the internal implementation and shows only the essential functionality to the user.
How to Achieve Abstraction in Java
Abstraction is achieved through:
- Interfaces — define what to do, not how
- Abstract Classes — partially implemented, partially abstract (covered in depth in later chapters)
Code Example Abstraction via Interface
1// Step 1: Define the Interface (what the user sees)2interface Car {3 void applyBrake();4 void pressAccelerator();5 void pressHorn();6}7
8// Step 2: Implement the Interface (hidden from user)9class CarImplementation implements Car {10
11 @Override12 public void applyBrake() {13 // Internal implementation — user doesn't see this14 // Step 1: Reduce fuel supply15 // Step 2: Engage hydraulic brakes16 // Step 3: ABS kicks in17 // Step 4: Friction pads engage18 // Step 5: Speed reduces19 System.out.println("Brake applied. Speed reduced.");20 }21
22 @Override23 public void pressAccelerator() {24 System.out.println("Accelerating...");25 }26
27 @Override28 public void pressHorn() {29 System.out.println("Beep beep!");30 }31}32
33// Step 3: User interacts only with the interface34public class Main {35 public static void main(String[] args) {36 Car myCar = new CarImplementation();37 myCar.applyBrake(); // User just calls this38 myCar.pressAccelerator(); // User just calls this39 }40}Brake applied. Speed reduced. Accelerating...
Line-by-Line Explanation
| Line | Explanation |
|---|---|
interface Car | Declares an interface — only method signatures, no implementation |
void applyBrake() | Method signature only — no body allowed in an interface (by default) |
class CarImplementation implements Car | Concrete class that provides the actual implementation |
@Override | Annotation indicating this method overrides the interface's method |
Car myCar = new CarImplementation() | User holds a reference of type Car — only sees interface methods |
Advantages of Abstraction
| Advantage | Explanation |
|---|---|
| Security | Internal steps are hidden from external code |
| Confidentiality | Sensitive logic cannot be accessed or misused |
| Simplicity | Client code is cleaner — fewer things to know |
| Flexibility | Implementation can change without breaking client code |
Flowchart How Abstraction Works
Interview Notes Abstraction
Simple answer for interview:
Abstraction is showing only what is needed, and hiding how it works. Like a TV remote — you press volume up, it increases. You don't know the infrared signals, the receiver circuit, or the firmware. That's abstraction.
How to achieve: Through interface and abstract class.
Pillar 2 Data Encapsulation
Overview
Encapsulation is the bundling of data (fields) and the methods that operate on that data into a single unit called a class. It is also referred to as data hiding because the class controls how its data is accessed and modified by external code.
Why This Concept Exists
- Gives the class full control over its own data
- Prevents uncontrolled modification of data by outside code
- Promotes loose coupling between classes
- Makes code easier to maintain and debug
Real-world Analogy
Think of a medicine capsule. The actual medicine (data) is inside the capsule. The capsule shell is the class — it protects the medicine inside and controls how it is released. You don't directly touch the medicine; it is administered through the capsule's design.
Definition
Encapsulation
bundles data (fields) and the methods that operate on that data into a single unit (class), providing full control over how data is accessed or modified.
Code Example Encapsulation
1class Dog {2 // Data Member (private = hidden from outside)3 private String color;4
5 // Data Method — Getter (public = accessible from outside)6 public String getColor() {7 return color;8 }9
10 // Data Method — Setter (public = accessible from outside)11 public void setColor(String color) {12 this.color = color;13 }14}15
16public class Main {17 public static void main(String[] args) {18 Dog rottweiler = new Dog(); // Create object19 rottweiler.setColor("black"); // Set color via method20 System.out.println(rottweiler.getColor()); // Get color via method21 }22}black
Access Modifiers The Control Mechanism
Access modifiers control who can access fields and methods. This is how encapsulation enforces data hiding.
| Modifier | Same Class | Same Package | Subclass | Any Class |
|---|---|---|---|---|
private | ✅ | ❌ | ❌ | ❌ |
default (no keyword) | ✅ | ✅ | ❌ | ❌ |
protected | ✅ | ✅ | ✅ | ❌ |
public | ✅ | ✅ | ✅ | ✅ |
private — hide the data
- Make getter/setter methods public — expose controlled accessWhat Happens Without Encapsulation?
1// BAD — No encapsulation2class Dog {3 public String color; // Anyone can change this directly4}5
6public class Main {7 public static void main(String[] args) {8 Dog d = new Dog();9 d.color = "INVALID_COLOR_12345"; // No validation, no control!10 }11}With encapsulation, you can add validation inside the setter:
1public void setColor(String color) {2 if (color != null && !color.isEmpty()) {3 this.color = color;4 } else {5 System.out.println("Invalid color.");6 }7}Can Another Class Access a Private Field?
This was a great question raised during the lecture. Let's fully address it.
1class Cat {2 void doSomething() {3 Dog rottweiler = new Dog();4 rottweiler.setColor("black"); // ✅ Allowed — method is public5 // rottweiler.color = "black"; // ❌ Compile error — field is private6 String col = rottweiler.getColor(); // ✅ Allowed — method is public7 System.out.println("Dog color: " + col);8 }9}Dog class controls its own color. The Cat class can request the color, but cannot directly manipulate it.Encapsulation Promotes Loose Coupling
Interview Notes Encapsulation
Key differentiation — Abstraction vs Encapsulation:
| Abstraction | Encapsulation | |
|---|---|---|
| What it does | Hides complexity / implementation | Hides data |
| Focus | What is exposed to user | How data is protected |
| Achieved via | Interface, Abstract Class | Private fields + public methods |
| Analogy | Car brake pedal hides engine internals | Medicine capsule protects medicine |
Pillar 3 Inheritance
Overview
Inheritance is the mechanism by which one class (child/subclass) acquires the properties and behaviors of another class (parent/superclass). The child class gets everything the parent has, and can also add its own new properties and behaviors.
Why This Concept Exists
- Code reusability — don't rewrite what already exists in the parent
- Hierarchical classification — models real-world "is-a" relationships
- Foundation for Polymorphism — overriding requires inheritance
Real-world Analogy
A child inherits traits from their parent — eye color, height tendency, talents. The child also has their own unique traits. Similarly, a subclass inherits fields and methods from the parent class, while also having its own additions.
Syntax
1class Parent {2 // parent members3}4
5class Child extends Parent {6 // inherited members from Parent + own members7}The keyword extends is used to establish inheritance.
Code Example Basic Inheritance
1// Parent Class2class Vehicle {3 boolean hasEngine;4
5 boolean getEngine() {6 return hasEngine;7 }8}9
10// Child Class11class Car extends Vehicle {12 String type; // e.g., "Hatchback", "SUV", "Sedan"13
14 String getCarType() {15 return type;16 }17}18
19public class Main {20 public static void main(String[] args) {21 Car swift = new Car();22 swift.hasEngine = true;23 swift.type = "Hatchback";24
25 System.out.println(swift.getEngine()); // ✅ Inherited from Vehicle26 System.out.println(swift.getCarType()); // ✅ Own method27
28 Vehicle myVehicle = new Vehicle();29 // myVehicle.getCarType(); // ❌ Compile error — parent can't access child methods30 }31}true Hatchback
Key Rules of Inheritance
| Rule | Explanation |
|---|---|
| Child accesses parent members | ✅ Child can use all non-private parent fields and methods |
| Parent accesses child members | ❌ Parent cannot access child-specific members |
extends keyword | Used by a class to inherit from another class |
Single parent only (via extends) | A class can extend only ONE class in Java |
private members not inherited | Private fields/methods are not directly accessible in child |
Types of Inheritance in Java
| Type | Description | Allowed in Java? |
|---|---|---|
| Single | One parent, one child | ✅ Yes |
| Multilevel | A → B → C (chain) | ✅ Yes |
| Hierarchical | One parent, multiple children | ✅ Yes |
| Multiple | One child, multiple parents | ❌ Not via extends |
The Diamond Problem
Multiple inheritance via extends is NOT allowed in Java. Here's why:
1class A {2 boolean getEngine() {3 return true; // implementation A4 }5}6
7class B {8 boolean getEngine() {9 return false; // implementation B10 }11}12
13// HYPOTHETICAL — This is NOT valid Java14// class C extends A, B { ... }15
16// If it were allowed:17// C obj = new C();18// obj.getEngine(); // Which one? A's or B's? AMBIGUITY!This ambiguity is called the Diamond Problem. Java avoids it by not allowing a class to extend more than one class.
Solving Diamond Problem via Interface
1interface A {2 void getEngine(); // No implementation — just declaration3}4
5interface B {6 void getEngine(); // No implementation — just declaration7}8
9// This IS valid in Java10class C implements A, B {11 @Override12 public void getEngine() {13 // C provides its OWN single implementation14 System.out.println("C's engine implementation");15 }16}Because interfaces don't have implementation (only method signatures), there is no ambiguity — class C is forced to provide its own single implementation.
Advantages of Inheritance
- Code Reusability — write once in parent, reuse in all children
- Enables Polymorphism — method overriding requires inheritance
- Logical Hierarchy — mirrors real-world classification (Vehicle → Car → ElectricCar)
Interview Notes Inheritance
Pillar 4 Polymorphism
Overview
Polymorphism comes from Greek: poly = many, morphism = forms. It means the same method can behave differently in different situations.
A person can be a father, a husband, and an employee — the same person taking different forms depending on context. Water can be liquid, solid, or gas — same substance, different forms.
Two Types of Polymorphism
Type 1 Method Overloading (Compile-Time / Static Polymorphism)
◆ Definition
Method Overloading means having multiple methods with the same name in the same class, but with different parameters (different type, different number, or different order).
Java resolves which method to call at compile time — hence "compile-time polymorphism."
◆ Syntax
1class Sum {2 // Same method name — different parameter types3 int doSum(int a, int b) {4 return a + b;5 }6
7 String doSum(String a, String b) {8 return a + b;9 }10
11 // Same method name — different number of parameters12 int doSum(int a, int b, int c) {13 return a + b + c;14 }15}◆ Code Example with Output
1public class Main {2 public static void main(String[] args) {3 Sum calc = new Sum();4
5 System.out.println(calc.doSum(5, 2)); // calls doSum(int, int)6 System.out.println(calc.doSum("Hello ", "World")); // calls doSum(String, String)7 System.out.println(calc.doSum(3, 4, 2)); // calls doSum(int, int, int)8 }9}7 Hello World 9
◆ How Java Resolves Overloading (Compile Time)
At compile time, the Java compiler looks at the argument types and count and resolves which method to invoke. The decision is made before the program even runs.
◆ Can You Overload by Return Type Only?
NO. This is a very common interview trap.
1class Sum {2 int doSum(int x, int y) {3 return x + y;4 }5
6 // ❌ NOT valid overloading — same name, same parameters, only return type differs7 String doSum(int x, int y) {8 return String.valueOf(x + y);9 }10}calc.doSum(5, 2) without capturing the return value, the compiler cannot determine which method to invoke — both match the argument signature. Return type is not part of the method's binding signature.◆ Overloading Rules Summary Table
| Rule | Allowed? |
|---|---|
| Different number of parameters | ✅ Yes |
| Different types of parameters | ✅ Yes |
| Different order of parameter types | ✅ Yes |
| Different return type only | ❌ No |
| Different parameter names only | ❌ No |
| Inside the same class | ✅ Yes (required) |
Type 2 Method Overriding (Runtime / Dynamic Polymorphism)
◆ Definition
Method Overriding means a child class provides its own implementation of a method that is already defined in the parent class. The method signature must be exactly the same — same name, same return type, same parameters.
Java resolves which method to call at runtime based on the actual object — hence "runtime polymorphism."
◆ Syntax
1class A {2 boolean getEngine() {3 return true; // Parent's implementation4 }5}6
7class B extends A {8 @Override9 boolean getEngine() {10 // Child's own implementation11 // Performs different computation12 return false;13 }14}◆ Code Example with Output
1class A {2 void getEngine() {3 System.out.println("A's engine — always available");4 }5}6
7class B extends A {8 @Override9 void getEngine() {10 System.out.println("B's engine — high performance");11 }12}13
14public class Main {15 public static void main(String[] args) {16 A objA = new A();17 objA.getEngine(); // Calls A's method18
19 B objB = new B();20 objB.getEngine(); // Calls B's overridden method21
22 A ref = new B(); // Polymorphic reference23 ref.getEngine(); // Calls B's method at RUNTIME24 }25}A's engine — always available B's engine — high performance B's engine — high performance
ref.getEngine() is the key insight: even though ref is declared as type A, at runtime the JVM looks at the actual object (new B()) and calls B's method. This is dynamic dispatch.◆ How Java Resolves Overriding (Runtime)
◆ Method Lookup Order
When obj.method() is called:
- JVM checks what the actual object type is (not the declared reference type)
- Looks for the method in that class first
- If not found, goes up the inheritance chain to the parent
◆ Overloading vs Overriding Master Comparison
| Feature | Method Overloading | Method Overriding |
|---|---|---|
| Also called | Static / Compile-time Polymorphism | Dynamic / Runtime Polymorphism |
| Where | Same class | Parent + Child class |
| Method name | Same | Same |
| Parameters | Must differ | Must be exactly the same |
| Return type | Can differ (if params also differ) | Must be same (or covariant) |
| Resolved at | Compile time | Runtime |
| Requires inheritance | ❌ No | ✅ Yes |
@Override annotation | Not used | Should be used |
| Access modifier | Can differ | Cannot be more restrictive |
◆ Interview Notes Polymorphism
5. OOP Relationships
Understanding relationships between classes is the foundation of Low Level Design (LLD). Two fundamental relationships in OOP are:
- IS-A (Inheritance)
- HAS-A (Association)
IS-A Relationship (Inheritance)
Definition
An IS-A relationship means one class is a type of another class. It is achieved through inheritance (extends).
Examples
| Statement | Interpretation |
|---|---|
Car extends Vehicle | Car IS-A Vehicle ✅ |
Dog extends Animal | Dog IS-A Animal ✅ |
ElectricCar extends Car | ElectricCar IS-A Car ✅ |
extends and the parent-child class hierarchy.HAS-A Relationship (Association)
Definition
A HAS-A relationship means one class contains an object of another class as a field (data member). This is also called Association or Composition/Aggregation depending on the strength of the relationship.
Example
1class Engine {2 int cc;3 String type;4}5
6class Bike {7 String brand;8 Engine engine; // Bike HAS-A Engine9}10
11class School {12 String name;13 List<Student> students; // School HAS-A list of Students14}Types of HAS-A Relationships
| Type | Multiplicity | Example |
|---|---|---|
| One-to-One | 1 student → 1 course | A student enrolled in exactly one course |
| One-to-Many | 1 student → many courses | A student enrolled in multiple courses |
| Many-to-Many | many students ↔ many courses | Each course has many students; each student has many courses |
Aggregation vs Composition
Both are forms of the HAS-A relationship, but they differ in the strength of dependency between the two objects.
Aggregation Weak HAS-A Relationship
◆ Definition
Aggregation is a HAS-A relationship where both objects can exist independently. Destroying one object does not destroy the other.
◆ Example School and Student
A School has Students. But if a school is closed, the students don't cease to exist — they go to another school. And if a student leaves, the school continues to exist.
1class Student {2 String name;3 int age;4}5
6class School {7 String name;8 List<Student> students; // Aggregation — weak relationship9}Ifschoolobject is destroyed → students still exist If allstudentobjects are removed → school still exists (empty)
Composition Strong HAS-A Relationship
◆ Definition
Composition is a HAS-A relationship where the contained object cannot exist without the container. Destroying the container also destroys the contained objects.
◆ Example School and Rooms
A school has rooms (classrooms). If the school building is demolished, the rooms are demolished too. The rooms have no independent existence outside of the school.
1class Room {2 int roomNumber;3 int capacity;4}5
6class School {7 String name;8 List<Room> rooms; // Composition — strong relationship9 // Rooms are created inside School and die with it10}Ifschoolobject is destroyed →roomsobjects are also destroyed Rooms cannot exist independently of the School
Aggregation vs Composition Summary Table
| Feature | Aggregation | Composition |
|---|---|---|
| Also called | Weak HAS-A | Strong HAS-A |
| Dependency | Weak — independent existence | Strong — dependent existence |
| Object lifecycle | Objects exist independently | Child dies with parent |
| Example | School HAS-A Student | School HAS-A Room |
| Relationship type | "Uses" | "Owns" |
Diagram Relationships Overview
Interview Notes Relationships
6. Interview Question Bank
Foundational Questions
| Question | Key Points to Cover |
|---|---|
| What is OOP? | Object-oriented, everything revolves around objects, objects have properties and behavior |
| What is a class? | Blueprint/template, contains data variables and data methods |
| What is an object? | Real-world entity, instance of a class, has properties and behavior |
| What is the difference between a class and an object? | Class is blueprint (method area), object is instance (heap memory) |
| What is the difference between procedural and OOP? | Procedural = function-first, data moves freely; OOP = data-first, bundled with methods |
Four Pillars Questions
| Pillar | Key Interview Question | Answer Hint |
|---|---|---|
| Abstraction | Explain with a real-world example | Car brake, phone call — expose only essential |
| Abstraction | How to achieve in Java? | Interface and Abstract Class |
| Encapsulation | What is data hiding? | Private fields + public getters/setters |
| Encapsulation | What are access modifiers? | private, default, protected, public |
| Inheritance | Types of inheritance in Java? | Single, multilevel, hierarchical — NOT multiple via extends |
| Inheritance | What is Diamond Problem? How solved? | Ambiguity with multiple parents; solved via interfaces |
| Polymorphism | Overloading vs Overriding | Compile-time vs runtime, same class vs parent-child |
| Polymorphism | Can we overload by return type only? | NO — compiler uses parameters for binding, not return type |
Relationship Questions
| Question | Answer |
|---|---|
| What is IS-A? | Inheritance — child IS-A type of parent |
| What is HAS-A? | Composition/Aggregation — one class has an object of another |
| Aggregation vs Composition? | Aggregation = weak (objects independent); Composition = strong (child dies with parent) |
7. Master Summary
All Four Pillars Quick Revision
Quick Revision Bullets
- ✅ OOP = everything revolves around objects (properties + behavior)
- ✅ Class = blueprint; Object = instance with real data in heap
- ✅ Abstraction = hide implementation, expose essential → via
interface/abstract class - ✅ Encapsulation = bundle data + methods in a class →
privatefields +publicmethods → loose coupling - ✅ Inheritance = child gets parent members →
extends→ IS-A relationship → code reuse - ✅ Multiple inheritance via
extendsNOT allowed → Diamond Problem - ✅ Diamond Problem solved via
interface— no ambiguity since interface has no implementation - ✅ Polymorphism = same method, different forms
- ✅ Overloading = same name, different params, same class → compile-time
- ✅ Overriding = same name, same params, parent-child → runtime (dynamic dispatch)
- ✅ Cannot overload by return type only — Java doesn't use return type in method binding
- ✅ IS-A = Inheritance | HAS-A = Association (Aggregation/Composition)
- ✅ Aggregation = weak HAS-A (objects independent) | Composition = strong HAS-A (child dies with parent)
Relationships Cheat Sheet
interface and abstract class (next chapter — unlocks abstraction fully)
3. Learn this, super, constructors
4. Practice inheritance chains
5. Master polymorphism with runtime examples
6. Apply everything in Low Level Design (LLD) problemsEnd of Chapter — OOP Fundamentals in Java
Practice & Implement
Apply what you learned in OOP Fundamentals
Define an abstract class `Animal` with fields `name` and `sound`. Create concrete subclasses `Dog` and `Cat` that override `makeSound()`. Instantiate both and call `makeSound()` for each.
Rex says: Woof! Whiskers says: Meow!
Mastery check
How well can you recall this?
Finished this lesson?