Protocol Buffers — Defining and Generating Java Code
Fast recall
Quick revision
Core answer
.proto file declaring your messages and service contracts, then use the protoc compiler (typically via the protobuf-maven-plugin or Gradle's protobuf plugin) to auto-generate Java source files. The generated stubs include POJOs for messages and abstract base classes for services that you implement on the server side and use as type-safe clients.Tests hands-on familiarity with the protobuf toolchain, a prerequisite for any real gRPC work.
Interviewers want to see that you know the full loop from schema to compilable Java code.
Interviewers want to see that you know the full loop from schema to compilable Java code.
Interview-ready guide
Interview answer
Protocol Buffers are gRPC's schema and serialization format — the wire format that makes gRPC payloads 5–10× smaller than JSON. The workflow has three parts: writing the schema, generating code, and implementing the service.
The `.proto` schema:
- Define message types (data shapes) and a service contract (RPC methods) in a
.protofile undersrc/main/proto - The key insight is that field numbers — not field names — identify data on the wire; you can rename fields freely without breaking backward compatibility because the binary encoding uses numbers, not names
- Never reuse deleted field numbers — mark them with
reservedto prevent accidental reuse that would silently misinterpret old binary data from clients still on the previous schema
Code generation:
- Configure the
protobuf-maven-pluginwith theprotoc-gen-grpc-javaextension somvn compileautomatically runsprotocand drops generated stubs intotarget/generated-sources - No separate protoc installation needed — the plugin downloads the right binary for the current OS, keeping CI reproducible across developer machines
Service implementation:
- Extend the generated
ImplBaseclass, override your RPC methods, and drive responses throughStreamObserver— callonNextto send data andonCompletedto close the stream
Version alignment — the most dangerous footgun:
- The
protoccompiler version,protobuf-javaruntime, andgrpc-javastubs all need to be compatible with each other; a mismatch between the protoc version that generated stubs and the runtime library causes subtle serialization bugs that are hard to trace - Pin all three versions in the parent POM as a single discipline to prevent hours of debugging that nobody expects from a code generator
I recommend java_multiple_files = true in your .proto files so each message generates a separate Java file — without it, all messages nest inside one outer class, making imports verbose and IDE navigation painful in large schemas.
Protocol Buffers — Defining and Generating Java Code
Direct Answer
You write a .proto file declaring your messages and service contracts, then use the protoc compiler (typically via the protobuf-maven-plugin or Gradle's protobuf plugin) to auto-generate Java source files. The generated stubs include POJOs for messages and abstract base classes for services that you implement on the server side and use as type-safe clients.
Common Mistakes & Pitfalls
Interviewers want to see that you know the full loop from schema to compilable Java code.
Check your understanding
Answer these without looking back at the article.
- What does `java_multiple_files = true` do in the proto options?
- How do you handle backward-compatible schema evolution in protobuf?
- What is the difference between `protoc-gen-grpc-java` and the `grpc-kotlin` code generator?
Related interview prep categories
1Want a guided curriculum order?
These modules are also sequenced in our flagship backend interview track — open it when you prefer an ordered path instead of browsing by topic.