OOP in Java
7 pages · ~5 min✓ Reviewed
Start here. Next up: Strings and Immutability.
Classes and Objects Mental Model
1. Class, Object, Reference
- Class = blueprint;
newbuilds an object on the heap - The variable holds a reference, not the object
b = acopies the reference: both point to one object==compares references
java
Dog a = new Dog("Rex"); Dog b = a; b.name = "Max"; System.out.println(a.name); System.out.println(a == b);
one object, two references
output
Max
true2. Defaults and this
| Default | Unassigned use | |
|---|---|---|
| Field | 0, false, null | OK |
| Local | none | compile error |
this= the current objectthis.name = nameresolves name shadowingthis(...)chains to another constructor
Common mistake
== on objects asks "same reference?", not "same state?". Use equals for state.
3. Interview Point
- Class vs object? Class is the blueprint; an object is one instance of it.
- Where do objects live? On the heap; variables hold references.
- What does
==compare? References, not state. - Field vs local defaults? Fields get 0, false, null; locals get none.
Constructors and Initialization Order
1. Constructor Rules
- Same name as the class, no return type
- Write none: compiler adds a default no-arg constructor
- Write any: default is gone,
new Foo()fails unless declared - Explicit
super(...)/this(...)first (Java 25 allows a prologue that does not touchthis) - Implicit
super()fails if parent has no no-arg constructor
2. Initialization Order
- 1Static blocksonce, at class load
- 2Parent fields + blocks
- 3Parent constructor
- 4Child fields + blocks
- 5Child constructor
Access Modifiers, static and final
1. Visibility, widest to narrowest
| Modifier | Reach |
|---|---|
public | everywhere |
protected | same package + subclasses |
| package-private | same package (no keyword) |
private | same class only |
2. static and final
static= class-level: shared by all objects, used asClassName.member- A static method has no
thisand no direct instance fields
Inheritance, Abstract Classes and Interfaces
1. extends and super
extends: single class inheritancesuper.m()calls the parent versionsuper(...)calls the parent constructor (Java 25: a prologue not using this may precede)- private members are not inherited
2. Abstract class vs interface
| Abstract class | Interface | |
|---|---|---|
| State | fields allowed | constants only |
| Constructors | yes | no |
Overloading vs Overriding and equals/hashCode
1. Overloading vs Overriding
| Overloading | Overriding | |
|---|---|---|
| Signature | same name, different params | same signature, subclass |
| Resolved | compile time, declared types | runtime, actual object (dynamic dispatch) |
| Return type | alone is not enough | may be covariant |
- Always add
@Override - Access cannot be narrowed
- No broader checked exceptions
Java OOP Cheat Sheet
1. Modifiers and Keywords
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
private | yes | no | no | no |
| package-private | yes | yes | no | no |
protected | yes | yes | yes | no |
public | yes | yes | yes |
Check yourself
Overloads f(Object) and f(String) exist. Which one does this call?
f(Object)- Overloads are chosen at compile time by the declared type, not the actual object
Object x = "hi";
f(x);What does new B() print?
0- A's constructor runs before B's fields are set, but the override still runs
class A { A() { show(); } void show() {} } class B extends A { int n = 5; void show() { System.out.println(n); } }
A class overrides equals on id but not hashCode. Does a HashSet find an equal copy of an added object?
- Usually no:
containsreturns false - Different hash codes usually land in different buckets, so
equalsnever runs