Java Programming - OOP Deep Dive
## Java Programming - OOP Deep Dive (Inspired by GoalKicker & Darshan PDF)
This chapter blends conceptual clarity from Darshans Java interview insights with professionally structured,
detailed GoalKicker-style formatting to form a master-level understanding of Java OOP.
---
### Chapter 3: Object-Oriented Programming (OOP) in Java
#### 3.1 What is OOP?
Object-Oriented Programming (OOP) is a paradigm centered around **objects**, which are instances of
**classes**. These objects encapsulate **data (fields)** and **behavior (methods)**.
**Core Concepts:**
- Real-world modeling (e.g., Car, Person, Animal)
- Code reusability
- Maintenance and scalability
**Main Pillars:**
1. Encapsulation
2. Abstraction
3. Inheritance
4. Polymorphism
---
Page 1
Java Programming - OOP Deep Dive
#### 3.2 Encapsulation
Encapsulation is the technique of **binding data and methods** that act on that data within a class and
**restricting direct access** to some of the object's components.
**Why?**
- Protect internal state (data hiding)
- Promote modularity and debugging
- Enable validation
**Syntax:**
```java
public class BankAccount {
private double balance;
public double getBalance() {
return balance;
public void deposit(double amount) {
if (amount > 0) balance += amount;
```
**Real-World Analogy:** ATM machines restrict users from seeing how money is dispensed internally.
Page 2
Java Programming - OOP Deep Dive
---
#### 3.3 Abstraction
Abstraction is the process of hiding implementation details and showing **only essential features**.
**Achieved by:**
- Abstract Classes
- Interfaces
**Example (Abstract Class):**
```java
abstract class Shape {
abstract void draw();
```
**Example (Interface):**
```java
interface Drawable {
void draw();
```
**Use Cases:**
- Framework design
- API specification
Page 3
Java Programming - OOP Deep Dive
- Driver and device interface
---
#### 3.4 Inheritance
Inheritance allows a new class (subclass/child) to acquire the properties and behaviors of an existing class
(superclass/parent).
**Syntax:**
```java
class Animal {
void eat() { System.out.println("eating..."); }
class Dog extends Animal {
void bark() { System.out.println("barking..."); }
```
**Types Supported in Java:**
- Single
- Multilevel
- Hierarchical
**Not Supported:** Multiple (to avoid ambiguity) use interfaces instead.
Page 4
Java Programming - OOP Deep Dive
---
#### 3.5 Polymorphism
Polymorphism allows the same interface to be used for different types ("many forms").
##### Compile-Time Polymorphism (Method Overloading)
```java
class MathUtil {
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
```
##### Run-Time Polymorphism (Method Overriding)
```java
class Animal {
void sound() { System.out.println("Animal makes sound"); }
class Dog extends Animal {
void sound() { System.out.println("Dog barks"); }
```
**Example Use:**
Javas `println()` method is overloaded for different data types.
Page 5
Java Programming - OOP Deep Dive
---
#### 3.6 Interface vs Abstract Class
| Feature | Interface | Abstract Class |
|------------------------|--------------------------------------|----------------------------------|
| Inheritance | Multiple allowed | Only single inheritance |
| Method Types | abstract, default, static (Java 8+) | abstract + concrete |
| Variables | public static final only | all types |
| Constructors | Not allowed | Allowed |
| Use case | Unrelated behavior (capability) | Shared base class functionality |
**Example:**
- `Runnable` (interface)
- `HttpServlet` (abstract class)
---
#### 3.7 IS-A vs HAS-A
- **IS-A**: Dog IS-A Animal (via `extends`)
- **HAS-A**: Car HAS-A Engine (via composition)
```java
class Engine {
void start() { System.out.println("Engine started"); }
class Car {
Page 6
Java Programming - OOP Deep Dive
Engine engine = new Engine();
void run() { engine.start(); }
```
---
#### 3.8 Real-World OOP Design: Smart ATM System
| OOP Principle | Application |
|----------------|------------------------------------------|
| Encapsulation | balance, pin as private fields |
| Abstraction | Only methods like deposit(), withdraw() |
| Inheritance | ATM SmartATM |
| Polymorphism | Different ATM types override withdraw() |
---
#### 3.9 Final Keyword Recap
- `final class` cant be extended
- `final method` cant be overridden
- `final variable` constant
```java
final class Config {}
final int PORT = 8080;
```
Page 7
Java Programming - OOP Deep Dive
Use for constants, security, and immutability.
---
#### 3.10 Composition vs Aggregation
| Composition | Aggregation |
|-----------------------------------|--------------------------------------|
| Strong association | Weak association |
| Contained object can't exist alone| Contained object can exist alone |
| Example: Car Engine | Example: Department Professor |
---
#### 3.11 Summary & Key Interview Traps
- Avoid deep inheritance chains
- Favor composition over inheritance for flexibility
- Interface is for capabilities; abstract class is for shared structure
- `private` + getter/setter = Encapsulation
- Abstract method = no body (ends in `;`)
Page 8