DEV Community

Cover image for OOP in Java: A First-Year Student’s Cheat Sheet
M.Kabileshwaran
M.Kabileshwaran

Posted on

OOP in Java: A First-Year Student’s Cheat Sheet

Think of OOP like building with blueprints and real-world creations.
1. Classes vs. Objects – The Simple Difference
A class is like a recipe for a cake. It lists the ingredients (data) and steps (actions). An object is the actual cake you bake using that recipe. You can make many cakes from one recipe, just like you can create many objects from one class.
Example:
Class (Recipe): "How to make a car" (has wheels, color, speed).
Object (Real Car): Your red Toyota (with 4 wheels, going 60 mph).
In Java:

  • Class = blueprint or template
  • Object = an instance of that blueprint, a real thing created based on the class

Example:
Think of a Car class — it describes what a car is (color, model, year) and what it can do (drive, brake). When you create a Car object, say myCar, you’re making a specific car, like a red 2020 Toyota.

class Car {
    String color;
    int year;

    void drive() {
        System.out.println("The " + color + " car is driving.");
    }
}
Enter fullscreen mode Exit fullscreen mode
public class Main {
    public static void main(String[] args) {
        Car myCar = new Car();  // Creating an object
        myCar.color = "Red";
        myCar.year = 2020;
        myCar.drive();  // Output: The Red car is driving.
    }
}
Enter fullscreen mode Exit fullscreen mode

2. What Are Constructors? Do We Have Destructors in Java Like C++?
Constructor:
Think of it as a special method that builds your object. When you create an object, the constructor sets up initial values — like choosing the color and year of your car right when you build it.

class Car {
    String color;
    int year;

    // Constructor
    Car(String c, int y) {
        color = c;
        year = y;
    }
}
Enter fullscreen mode Exit fullscreen mode

Destructors?
In C++, destructors clean up when an object is destroyed (like cleaning your room after playing). In Java, there are no destructors because Java has a garbage collector that automatically cleans up unused objects. So, you don’t have to worry about manually deleting objects.

3. What Is static?
static is like a shared property or method that belongs to the class itself, not to any specific object.
Example: Imagine a school where every student has a name (instance variable), but the school’s name is the same for all students (static variable).

class Student {
    String name;              // each student has a name
    static String school = "ABC High School";  // shared by all students
}
Enter fullscreen mode Exit fullscreen mode

You can access static stuff without creating an object:
System.out.println(Student.school); // Output: ABC High School

4. What Are Access Modifiers?

Access modifiers control who can see or use your class members (variables, methods). Think of them as privacy levels:

  • public: Everyone can see and use it — like a public park.
  • private: Only the class itself can see it — like your diary.
  • protected: The class and its subclasses (children) can see it — like family secrets.
  • default (no modifier): Only classes in the same package can see it — like your neighborhood friends.

5. Example Scenarios & Scope of Access Modifiers
Image description

6. The 4 Pillars of OOP and How to Implement Them in Java
Encapsulation — Wrapping data and methods together, hiding details. Use private variables + public getters/setters.

class Person {
    private String name;

    public String getName() { return name; }
    public void setName(String n) { name = n; }
}
Enter fullscreen mode Exit fullscreen mode

Inheritance — A class inherits properties/methods from another (like a child inheriting traits).

class Animal {
    void eat() { System.out.println("Eating"); }
}

class Dog extends Animal {
    void bark() { System.out.println("Barking"); }
}
Enter fullscreen mode Exit fullscreen mode

Polymorphism — One interface, many forms. Methods can behave differently in subclasses.

class Animal {
    void sound() { System.out.println("Some sound"); }
}

class Dog extends Animal {
    void sound() { System.out.println("Bark"); }
}
Enter fullscreen mode Exit fullscreen mode

Abstraction — Hiding complex details, showing only what’s necessary. Use abstract classes or interfaces.

abstract class Vehicle {
    abstract void move();
}

class Bike extends Vehicle {
    void move() { System.out.println("Bike is moving"); }
}
Enter fullscreen mode Exit fullscreen mode

7. How Are Objects Instantiated in Memory?
When you create an object with new, Java allocates memory for it on the heap (the big memory area for objects). The reference (like a pointer) to that object lives on the stack.
Example:

Car myCar = new Car("Blue", 2021);

  • myCar is a reference on the stack.
  • The actual Car object is stored on the heap.

8. Can We Pass Objects as Arguments to Other Objects? How?
Objects can be passed as parameters to methods or constructors, enabling interaction between objects.
Example: A Driver object can have a Car object passed to it.

class Car {
    String model;
    Car(String m) { model = m; }
}

class Driver {
    void drive(Car car) {
        System.out.println("Driving a " + car.model);
    }
}

public class Main {
    public static void main(String[] args) {
        Car myCar = new Car("Tesla");
        Driver d = new Driver();
        d.drive(myCar);  // Output: Driving a Tesla
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)