Skip to main content

Java OOP

Classes & Objects

Complete lesson for Classes & Objects using LEARNCODEGURU’s 6-level learning model — from basics to advanced practice, with Java OOP and Collections where relevant.

L1

Basics — Classes & Objects

Section: Java OOP. Classes as blueprints, objects on the heap, new, fields, and the Object root type.

Class vs object

  • A **class** is a type (blueprint): fields + methods. An **object** is an instance created at runtime with new.
  • The variable holds a *reference*. The object lives on the heap until nothing references it, then GC may reclaim it.
  • Every class (except Object) has a superclass. If you omit extends, you extend java.lang.Object.
public class Student {
    String name;
    int roll;

    void display() {
        System.out.println(roll + " " + name);
    }

    public static void main(String[] args) {
        Student s = new Student();
        s.name = "Neha";
        s.roll = 21;
        s.display();
    }
}

Creating objects

  • new Student() allocates heap memory, runs the constructor, and returns a reference.
  • Several references can point to one object (Student t = s;). Assigning s = null does not delete the object if t still refers to it.
  • Field defaults: 0 / false / null. Locals still need assignment.

Members

MemberBelongs toExample
Instance fieldEach objectString name;
Instance methodCalled on an objects.display()
Static fieldThe classstatic int count;
Nested typeThe classinner / static nested class
L2

Advanced — Classes & Objects

Identity, equals, and allocation

  • == on objects is identity. Override equals and hashCode together when value equality matters (Objects.hash).
  • new always creates a new object (except interned strings / cached wrappers you did not new).
  • A class can have many objects; objects do not exist without a class (or an anonymous/local class, which still has a class file).
  • Records (Java 16+) are compact immutable data classes with generated equals/hashCode/toString.
L3

Tricky Interview Points

Q1) What is a class? What is an object?

Answer A class is a user-defined type. An object is an instance of that type created at runtime, typically with new.

Q2) Where is the object stored?

Answer On the heap. The stack (or another heap field) stores the reference.

Q3) What does new do?

Answer Allocates memory, initializes fields to defaults, runs the constructor, and returns the reference.

Q4) Can two references point to one object?

Answer Yes. Assignment copies the reference, not the object (unless you clone or copy fields).

Q5) What is the root of the class hierarchy?

Answer java.lang.Object. It provides equals, hashCode, toString, getClass, wait/notify, and clone (protected).

Q6) Is a class an object?

Answer At runtime there is a Class<?> object describing the type (reflection), but that is not the same as saying “the class is an instance of itself”.

L4

Simple Programs

Two objects, independent state

public class Account {
    String owner;
    double balance;

    void deposit(double amount) {
        balance += amount;
    }

    public static void main(String[] args) {
        Account a = new Account();
        a.owner = "Ravi";
        a.deposit(500);
        Account b = new Account();
        b.owner = "Mia";
        b.deposit(120);
        System.out.println(a.owner + " " + a.balance);
        System.out.println(b.owner + " " + b.balance);
    }
}
L5

Problem Solving Tricks

  • Initialize fields in the constructor or at declaration; do not leave “set everything from main”.
  • Override toString early — debugging gets easier.
  • Prefer private fields from the first OOP lesson so encapsulation is a habit.
  • One concept per class. Student should not also parse files and send email.
L6

Practice Programs List

  1. Model a Book with title, price, and a discountedPrice method.
  2. Create two Book objects and print them via a custom toString.
  3. Assign Book copy = original; then change copy.price — observe they share state.
  4. Add a static int created incremented in the constructor (next lesson will refine this).
  5. Write a Point class with distanceTo(Point other).