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

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)

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:

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:

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

FeatureWeb ServerApplication Server
Serves Static ContentYes (HTML/CSS/JS)No
Serves Dynamic ContentNoYes (Java, Spring)
ExamplesApache, NGINXTomcat, 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

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


4
Subscribe to my newsletter

Read articles from TechEazy Consulting directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

TechEazy Consulting
TechEazy Consulting