Why this matters
- Non-static inner classes hold a hidden reference to their enclosing instance, which is a real and common cause of memory leaks.
- Immutability removes the need for synchronisation entirely, which is why it is the foundation of safe concurrent code.
- Records, covered in the Java 17 module, are the language's answer to the immutable-class boilerplate shown here — but the manual recipe is still what you need for anything with more than plain data.
The class kinds interviewers name
Every class sits in a family that starts at Object. The words below are roles, and a single class can wear more than one.
Object— the root. Every class extends it, directly or through a chain, and inheritsequals,hashCode,toStringand the monitor methods. There is no class above it.- Superclass and subclass — the parent you
extend, and the child that extends you. Java allows one superclass.superreaches the parent's constructor or a hidden member;thisis the current object. The inheritance lesson works through when that relationship is the wrong tool. - Concrete class — a class you can
new. All of its methods have bodies. - Abstract class — cannot be instantiated. It may mix finished methods, fields and constructors with abstract methods a subclass must implement. Covered with interfaces.
- Final class —
final classcannot be extended.Stringand the primitive wrappers are final so their contracts cannot be broken by a subclass. A final class is also the usual shape of an immutable type. - POJO — a "plain old Java object": fields, constructors and accessors, with no required framework base class. A JavaBean is a narrower convention on top of that: a public no-arg constructor, and getters and setters named
getX/setX. A POJO does not have to be a bean. - Enum — a class with a fixed set of instances. Constants are public static final, the constructor is implicitly private, and
==is correct. Enums are introduced with the other types in the types lesson and specialised asEnumSetandEnumMaplater. - Nested class — a class declared inside another. The four forms are next.
Four kinds of nested class
static nestedNo outer instance needed
inner (non-static)Holds a hidden outer reference
localDeclared inside a method
anonymousOne-off implementation
- Static nested class — declared
static. It has no link to an enclosing instance and behaves like a top-level class that happens to live inside another for namespacing.Map.Entryis the familiar example. - Inner (non-static) class — holds an implicit reference to the enclosing object, so it can read its private fields. It cannot exist without an instance of the outer class.
- Local class — declared inside a method body. Visible only there.
- Anonymous class — declared and instantiated in one expression, with no name. Largely replaced by lambdas for single-method interfaces.
Loading compiler…
Creating them differs, and the inner-class syntax surprises people:
Loading compiler…
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 4 sections
- Anonymous classes versus lambdas
- Immutable objects
- What immutability buys you
- Common misreadings
Test yourself
Answer these before moving on — recall is what makes it stick.