7Why do static, private and final Java methods behave differently from ordinary overridable instance methods in a subclass?
An ordinary overridable instance method participates in dynamic dispatch: after compile-time method selection, the implementation depends on the receiver object's runtime class. A static method belongs to a class and is selected using compile-time type information; a subclass may hide an accessible static method, but this is not polymorphic overriding. I would call it through the class name to make that explicit. A private superclass method is not inherited as an overridable member; a same-named method in the subclass is a separate declaration, and superclass calls to its private method still use that method. A final method may be inherited and called but cannot be overridden or hidden by a subclass. These rules differ from overloading, which introduces a different parameter signature and is resolved at compile time. @Override helps catch mistaken assumptions for instance methods.
Try answering this question with an AI coach
8What is the difference between method overloading and overriding?
Overloading means several methods have the same name but different parameters; the compiler chooses the method based on static argument types. Overriding means a subclass replaces the implementation of an instance method from a parent class; runtime dispatch chooses the method based on the actual object type. Static methods are hidden, not overridden, and private methods are not overridden because they are not inherited as accessible members.
void send(String message) {}
void send(String message, int priority) {}
class Animal {
void speak() { System.out.println("Animal"); }
}
class Dog extends Animal {
@Override
void speak() { System.out.println("Dog"); }
}
Try answering this question with an AI coach
9What is the difference between an interface and an abstract class?
An interface defines a contract and can contain abstract methods, default methods, static methods, constants and private helpers. An abstract class can additionally have instance state, constructors, protected fields, shared implementation and abstract or concrete methods. A class can extend only one class but implement multiple interfaces. Use an interface for an independent contract and an abstract class for closely related types with shared implementation and state.
interface Repository<T> {
Optional<T> findById(long id);
}
Try answering this question with an AI coach
10What does the final keyword mean?
A final variable can be assigned only once, but a final reference does not make the referenced object immutable. A final method cannot be overridden. A final class cannot be extended. final communicates intent and limits reference changes or inheritance, but by itself it does not provide a complete immutable model or thread safety.
final int maxAttempts = 3;
final List<String> names = new ArrayList<>();
names.add("Alice"); // allowed
public final void validate() {}
public final class Token {}
Try answering this question with an AI coach
11What are records in Java?
A record is a compact syntax for immutable data carriers. The compiler generates private final fields, constructor, accessors, equals, hashCode and toString. Records are useful for DTOs, value objects and method results. They are not a replacement for every domain entity: validation can be added in a compact constructor, but complex mutable behavior, lazy loading and inheritance-based models usually fit records poorly.
public record User(long id, String name) {}
public record Order(List<String> items) {
public Order {
items = List.copyOf(items);
}
}
Try answering this question with an AI coach
12How do you create an immutable object in Java?
I would define a final class, or otherwise tightly control subclass behavior, and initialize private final value fields completely in the constructor. Expose observations but no operations that change the value; transformations return a new instance. Prefer immutable component types. For mutable inputs such as arrays, take defensive copies, and never return a mutable internal alias from an accessor. Copying only a collection's outer structure is insufficient if its elements remain externally mutable. An unmodifiable view can still reflect changes through another reference, and a record's final components do not guarantee deep immutability. Validate constructor arguments and do not let this escape before construction finishes. Use safe publication when sharing the instance across threads. Equality and hashCode should reflect the stable value. Internal caching is possible only if it does not alter observable semantics and is itself safely implemented.
public final class User {
private final String name;
private final List<String> roles;
public User(String name, List<String> roles) {
this.name = name;
this.roles = List.copyOf(roles);
}
public List<String> getRoles() {
return roles;
}
}
Try answering this question with an AI coach