# Recap of OOPs in Java with Real-World Examples and a Simple Java Program

---

Object-Oriented Programming (OOPs) is the backbone of Java. If you're just starting out with Java or revisiting it after a while, this post will give you a strong foundation in OOPs and how it connects to building real-world applications like Dropbox.

---

## 🔹 What is a Class?

A **class** is a blueprint for creating objects. It defines properties (variables) and behavior (methods). Think of it like a template for a "Person" or a "Document" in a system.

---

## 🔹 Implementing a `Person` Class in Java

```java
public class Person {
    private String name;
    private int age;
    private String category;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        if (age >= 0 && age <= 120) {
            this.age = age;
            assignCategory();
        } else {
            System.out.println("Invalid age!");
        }
    }

    private void assignCategory() {
        if (age < 13) {
            category = "Child";
        } else if (age < 20) {
            category = "Teen";
        } else {
            category = "Adult";
        }
    }

    public String getCategory() {
        return category;
    }
}
```

---

## 🔹 OOPs Concepts in Action

### ✅ Encapsulation

By using `private` variables and `getters/setters`, we **control access** and **validate input**. For example, age must be between 0 and 120.

### ✅ Abstraction

The internal logic that assigns a `category` is hidden. Consumers of the class just see the result via `getCategory()`, without needing to know how it’s calculated.

---

## 🔹 Sample Java Program (Entry Point)

```java
public class Main {
    public static void main(String[] args) {
        Person p = new Person();
        p.setName("Alice");
        p.setAge(25);

        System.out.println(p.getName() + " is an " + p.getCategory());
    }
}
```

### 🔰 Entry Point

Java execution starts from the `main()` method. When you run this program, you'll see:

```plaintext
Alice is an Adult
```

---

## 🌐 What Happens When You Hit a URL?

When you open a browser and enter a URL:

1. DNS translates the domain to an **IP address**.
    
2. A **request** is sent to the server at that IP.
    
3. The **web server** (like Apache or NGINX) handles static files (HTML, CSS).
    
4. The **application server** (like Tomcat or Spring Boot) processes dynamic requests (Java code, database interaction).
    
5. A **response** is returned to your browser.
    

---

## 🔌 Simple Java Socket Server Example

To understand how content can be served, let’s look at a **barebones socket server** in Java:

```java
import java.io.*;
import java.net.*;

public class SimpleServer {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(8080);
        System.out.println("Listening on port 8080...");

        Socket client = server.accept();
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        PrintWriter out = new PrintWriter(client.getOutputStream(), true);

        String request = in.readLine();
        System.out.println("Request: " + request);

        out.println("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nHello from Java Server!");

        in.close();
        out.close();
        client.close();
        server.close();
    }
}
```

---

## 🔍 Web Server vs App Server

| Feature | Web Server | Application Server |
| --- | --- | --- |
| Serves Static Content | Yes (HTML/CSS/JS) | No |
| Serves Dynamic Content | No | Yes (Java, Spring) |
| Examples | Apache, NGINX | Tomcat, JBoss, Spring |

---

## 🌱 Why Spring Boot?

Manually writing socket code is fun—but painful! Spring Boot makes serving content, handling requests, managing dependencies, and building REST APIs super simple and productive.

---

## ☕ Why Java is a High-Level Language (Compared to C/C++)

* Automatic memory management (no manual `malloc`/`free`)
    
* Huge standard library (Collections, Networking, File I/O, Security)
    
* Platform-independent via JVM
    
* Built-in multithreading and exception handling
    

---

## 🧰 Java Libraries in Action

* `java.util.ArrayList` → for dynamic lists
    
* `java.net.Socket` → for network communication
    
* `java.nio.file` → for file operations
    
* `javax.servlet` / `org.springframework.web` → for web apps
    

---

## 📘 Assignment

### ✅ Basic

Design a **Document Management System** (like Dropbox):

* Identify a class: `Document`
    
* Properties: `String name`, `File file`, `String owner`, `Date uploadedAt`
    
* Modify `main()` to accept command-line arguments: document name & file path
    

```bash
java DocumentUploader "Resume.pdf" "/Users/you/Downloads/Resume.pdf"
```

### ✅ Advanced

Use GPT or Google to find how to read a file and store it in a given location using Java. We’ll discuss error handling and clean code in the next session.

💡 **Teaser**: We'll explore file uploads, exception handling, and REST APIs in our upcoming session.

---

## 🎓 Want to Learn More?

Join our **AWS Internship Program** to go from Java basics to cloud deployment.  
👉 [Register Now](https://techeazyconsulting.com/class-detail/AWS-Internship)

---
