Review POJO persistence, interface design, synchronization, casts, equality, observer callbacks, and map ordering in ten updated Java interview answers.
Answer: A POJO is an ordinary Java object whose design is not tied to a required framework superclass. Persistence stores its state beyond the current execution. Options include explicit JDBC mapping, an object-relational mapper using Jakarta Persistence, and application-defined file formats. The object having fields does not by itself make it persistent.
JDBC belongs to Java SE; Jakarta Persistence is a separate specification requiring a provider. Native Java serialization is another mechanism for eligible object graphs, but is not equivalent to relational persistence. An entity must meet the chosen framework's rules; an arbitrary record is not automatically a portable persistence entity.
Answer: An abstract class can hold per-instance state, declare constructors, and combine implemented and abstract methods. An interface defines a contract without per-instance fields or constructors. A class extends one superclass but can implement several interfaces.
Interfaces can contain abstract, default, static, and private methods. Their fields are public static final, although the referenced objects need not be immutable. An abstract class need not declare any abstract methods. Choose based on shared state and substitutability, rather than the obsolete claim that interfaces have no implementation.
Answer: Synchronization coordinates access to shared state. A synchronized block acquires an object monitor, provides mutual exclusion for code using that same monitor, and establishes visibility through the Java memory model. Unlocking a monitor happens-before a subsequent lock of that monitor.
All accesses participating in an invariant need a compatible coordination policy. Synchronizing one writer does not make an unsynchronized reader correct. Locks, atomic classes, immutable values, and concurrent collections address different coordination needs.
Answer: A protected member is accessible within its declaring package. Outside that package, subclass access is permitted subject to additional rules: for an instance member, the qualifying reference must have the accessing subclass type or a subtype of it. Subclass status does not grant access through every arbitrary superclass reference.
For example, inside an out-of-package subclass Child, access through this or a Child reference can be valid where access through a Parent reference is not. Protected constructors have their own access rules.
Answer: Upcasting treats a subtype value as a supertype and normally needs no explicit cast. Downcasting narrows the reference type and may require a runtime check. A checked cast fails with ClassCastException when a non-null object has an incompatible runtime type; it does not transform the object.
public class SafeCasts {
static class Shape { }
static final class Circle extends Shape {
String describe() { return "Circle"; }
}
public static void main(String[] args) {
Shape shape = new Circle();
if (shape instanceof Circle circle) {
System.out.println(circle.describe());
}
Shape plain = new Shape();
System.out.println(plain instanceof Circle);
}
}
The original idea of constructing a plain Shape and casting it into a Circle cannot work. Pattern matching tests compatibility and supplies the narrowed variable together.
Answer: It returns an int used by hash-based structures to distribute keys or elements. Equal objects must have equal hashes, but unequal objects can collide. The hash is not a unique identifier or guaranteed memory address. When defining value equality, provide a compatible hashCode and keep key equality state stable while stored.
Answer: Both are resizable lists. Vector synchronizes many individual operations; ArrayList does not. That does not make a sequence of Vector calls automatically atomic. Both new empty lists have size zero: capacity is backing storage, not the number of elements.
ArrayList is commonly used for ordinary list storage. For concurrent use, select suitable synchronization or a concurrent list implementation based on the workload. Do not infer performance or safety solely from the presence of synchronized methods.
Answer: An object registers interested listeners and calls them when a relevant event occurs. The legacy java.util.Observer and Observable types are deprecated. Alternatives include a small listener interface, PropertyChangeSupport for property events, or Flow for reactive-stream-style signaling with demand management.
Choose a mechanism that fits the event model. State whether callbacks run synchronously, how listeners are removed, and how callback exceptions are handled. Browser click events are an analogy, not the definition of Java's Observer API.
Answer: HashMap offers expected constant-time basic key operations with suitable hashes and no iteration-order guarantee. TreeMap maintains comparison order on keys and offers logarithmic basic operations plus range/navigation methods. Neither is synchronized.
Both allow duplicate values but only one mapping per equivalent key. HashMap permits a null key; natural-order TreeMap does not. A TreeMap comparator can permit null if it explicitly defines that ordering. TreeMap is not the only SortedMap implementation.
Answer: It uses natural ordering of its keys through Comparable. The keys must be mutually comparable. Comparison equality determines whether two keys represent the same mapping, so an ordering consistent with equals is needed to follow the ordinary Map equality contract. Values need not be comparable.
References: Java SE 25 Language Specification, Object, HashMap, TreeMap, Observer, Jakarta Persistence 3.2.