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.
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 omitextends, you extendjava.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;). Assignings = nulldoes not delete the object iftstill refers to it. - Field defaults:
0/false/null. Locals still need assignment.
Members
| Member | Belongs to | Example |
|---|---|---|
| Instance field | Each object | String name; |
| Instance method | Called on an object | s.display() |
| Static field | The class | static int count; |
| Nested type | The class | inner / static nested class |
Advanced — Classes & Objects
Identity, equals, and allocation
==on objects is identity. OverrideequalsandhashCodetogether when value equality matters (Objects.hash).newalways creates a new object (except interned strings / cached wrappers you did notnew).- 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.
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”.
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);
}
} Problem Solving Tricks
- Initialize fields in the constructor or at declaration; do not leave “set everything from main”.
- Override
toStringearly — debugging gets easier. - Prefer
privatefields from the first OOP lesson so encapsulation is a habit. - One concept per class.
Studentshould not also parse files and send email.
Practice Programs List
- Model a
Bookwith title, price, and adiscountedPricemethod. - Create two
Bookobjects and print them via a customtoString. - Assign
Book copy = original;then changecopy.price— observe they share state. - Add a static
int createdincremented in the constructor (next lesson will refine this). - Write a
Pointclass withdistanceTo(Point other).