| Claim | Verdict |
|---|
| Constructors are inherited | False. Each class declares its own. |
| Private members are not inherited | False. They are, but not directly accessible. |
| A same-named field is overridden | False. It hides, leaving two fields. |
| Any same-named method is an override | False. The signature must match. |
| Inheritance is for code reuse | Partly. It is a type relationship first. |
| Java supports multiple inheritance | Only of type, through interfaces. |
| Interfaces exist to reduce coupling | False. They enable polymorphism. |
| Downcasting is checked at compile time | False. It fails at runtime. |
Frequently Asked Questions
What exactly does extends inherit?
Instance fields and methods, including their access modifiers, subject to reachability. Constructors are never inherited, static methods are hidden rather than overridden, and private members are inherited into the object but cannot be named by the subclass. Everything else follows the normal visibility rules.
Why does Java not allow multiple inheritance of classes?
Because two parents could each supply an implementation of the same method, and the compiler would have no principled way to choose. Java offers multiple inheritance of type through interfaces instead, which sidesteps the conflict because interfaces contribute behaviour rather than stored state.
Is hiding a field the same as overriding a method?
No, and the difference is significant. Hiding creates a second field in the same object, so both values occupy memory and the parent version is unaffected. Overriding replaces one implementation with another. Hiding the same name is almost always a mistake, so prefer a distinct name or a method.
What is the root superclass of every Java class?
java.lang.Object, applied implicitly even without an extends clause. It is the source of methods such as getClass, toString, and equals, which is why toString prints an identity hash unless you override it.
When should I prefer composition over inheritance?
Whenever the relationship is HAS-A rather than IS-A, and whenever you would otherwise need multiple parents, since composition takes a constructor argument instead. Inheritance permanently couples the two types and lets a child override behaviour the parent relies on, so composition is the safer default.
Does a subclass see private fields of its parent?
The fields are present in the object, but the subclass cannot name them directly. It must reach them through a parent method, or the parent can widen access to protected for exactly this purpose. What matters is that private means private to the declaring class, not to the object.
Can a final class be extended?
No, and that is the point. A final class forbids extension entirely, while a final method forbids only overriding, so the class remains extensible. Sealed classes offer a middle ground, permitting only the subclasses you list explicitly.
What is upcasting and is it safe?
Upcasting is assigning a child instance to a parent-typed variable, and it is always safe at compile time because the child satisfies the parent contract. The reverse, downcasting, is checked at runtime and throws a ClassCastException unless the object really is that subtype.