What are the components of a Java class declaration, and what does a minimal class look like?
answer
- [modifiers] class Name [extends] [implements] { body }
- Minimal: class Dog {}
- Top-level modifiers: public/abstract/final/sealed — NOT private/protected
- No extends = implicitly extends Object
- Body = fields, methods, constructors, nested types
basics
~20 sA class declaration is the keyword class, a name, and a body in curly braces holding fields and methods. Optionally it has modifiers (like public) before it and an extends/implements clause. Minimal: class Dog { }.
solid answer
~40 sA top-level class declaration reads: optional modifiers, the class keyword, the class name, an optional generic type-parameter list, an optional extends clause (one superclass), an optional implements clause (one or more interfaces), then a body in braces. The body holds members: fields, methods, constructors, initializer blocks, and nested types. Legal top-level modifiers are public, final, abstract, sealed/non-sealed, and strictfp; private and protected are not allowed on a top-level class. By convention the name is PascalCase. A minimal valid class is just `class Dog {}` — no modifiers, no superclass (it implicitly extends Object), and an empty body. The compiler supplies a default no-arg constructor when you write none.
go deeper
Can write a class with the keyword, a PascalCase name, and braces, and add a field and method.
Knows the full clause order (modifiers/extends/implements), which modifiers are legal on a top-level class, and that omitting extends means Object.
Explains package-private default access, implicit Object superclass, default-constructor generation, and the role of sealed/abstract/final in expressing design intent.
Frames class structure in terms of API surface and encapsulation — chooses modifiers deliberately (final/sealed for closed hierarchies) and can teach the grammar precisely.
## What a class is In Java, a **class** is a blueprint that defines the structure (data) and behavior (methods) of objects. Every piece of code you write lives inside a class (or another type like an interface, enum, or record). Understanding the exact shape of a class declaration is the foundation for everything else in object-oriented Java. ## The grammar of a class declaration A **top-level class** (one written directly in a file, not nested inside another type) has this structure, reading left to right: ``` [modifiers] class Name [<TypeParameters>] [extends Superclass] [implements Interface1, Interface2, ...] { body } ``` Term by term: - **modifiers** — optional keywords that change visibility or behavior. For a top-level class the legal ones are: `public` (visible everywhere), `abstract` (cannot be instantiated, may have unimplemented methods), `final` (cannot be subclassed), `sealed`/`non-sealed` (restrict which classes may extend it, Java 17+), and `strictfp` (a now-rarely-used floating-point flag). You may NOT write `private` or `protected` on a top-level class — those only apply to *members* and *nested* classes. - **class** — the keyword that says "this is a class declaration" (as opposed to `interface`, `enum`, or `record`). - **Name** — the identifier. By the universal Java naming convention it is **PascalCase** (each word capitalized, e.g. `BankAccount`). This is a convention, not a compiler rule, but every codebase and tool expects it. - **<TypeParameters>** — optional generics, e.g. `class Box<T>`, letting the class work with a type supplied later. - **extends Superclass** — optional; names the single class this one inherits from. Java allows only **one** superclass (single inheritance of implementation). If you omit it, the class implicitly extends `java.lang.Object`, the universal root of all classes. - **implements ...** — optional; lists one or more interfaces whose contracts this class promises to fulfill. A class may implement many interfaces. - **{ body }** — curly braces enclosing the **members**: fields (variables that hold state), methods (behavior), constructors (initialization logic), static and instance initializer blocks, and nested types. ## The minimal class The smallest legal class is: ```java class Dog {} ``` This is complete and compiles. It has no modifiers (so it has *package-private* access — visible only within its own package), it implicitly extends `Object`, implements no interfaces, and has an empty body. Because you wrote no constructor, the compiler generates a **default no-argument constructor** for you, so `new Dog()` works. ## A fuller example ```java public final class BankAccount { private long balanceCents; // a field (state) public BankAccount(long opening) { // a constructor this.balanceCents = opening; } public long balanceCents() { // a method (behavior) return balanceCents; } } ``` Here `public` makes it visible everywhere, `final` forbids subclassing, the body holds one field, one constructor, and one method. ## Why this matters Knowing the precise anatomy lets you read any class at a glance, predict its visibility and inheritance, and avoid compiler errors like applying `private` to a top-level class. It also clarifies what is mandatory (the keyword, name, and braces) versus optional (everything else).
- What access does `class Dog {}` have with no modifier?Package-private (default): visible only to other code in the same package.
- What happens if you write no constructor at all?The compiler generates a default public no-arg constructor (with the same access as the class), so `new Dog()` works — unless you declare any other constructor, in which case it is not generated.
saying these in an interview costs you the question
- Saying a top-level class can be private or protected
- Claiming the class keyword or braces are optional
- Thinking a class with no extends has no superclass (it has Object)
- Confusing modifiers with the class name order