Java Inheritance and the extends Keyword - Quiz
Total: 6 questions
1. What is inheritance in Java and how is it declared with extends?
What is inheritance in Java and how is it declared with extends?
Inheritance is an object-oriented mechanism in which one class (the subclass) receives the fields and methods of another class (the superclass) and can add its own on top. It is declared with the extends keyword: class ColorBox extends Box6. A class can extend exactly one superclass, but a superclass may have any number of subclasses.
Inheritance expresses an IS-A relationship: a ColorBox IS-A Box6. If the sentence “X is a Y” sounds forced, use composition instead — a HAS-A relationship where an object holds another object in a field and delegates work to it. Every hierarchy ends at java.lang.Object: a class with no extends clause extends Object implicitly.
2. What does a subclass inherit from its superclass, and what is not inherited?
What does a subclass inherit from its superclass, and what is not inherited?
Inherited: public and protected fields and methods — usable in the subclass by name, as if declared there; static fields and methods — but they cannot be overridden, a static method with the same signature only hides the parent one. Members with no modifier (package-private) are visible to a subclass only inside the same package.
Not inherited: private fields and methods — they still occupy memory inside the subclass object, but cannot be referenced by name, only through a public or protected method of the superclass; constructors — a superclass constructor never becomes a constructor of the subclass, it is merely invoked from one through super(...).
3. What is the difference between upcasting and downcasting, and when do you get a ClassCastException?
What is the difference between upcasting and downcasting, and when do you get a ClassCastException?
Upcasting means a variable of the superclass type refers to a subclass object: Box6 box = new HeavyBox(15, 10, 20, 5);. It is implicit and always safe, because every HeavyBox really is a Box6. Polymorphism is built on it.
Downcasting is the opposite, explicit cast back to the subclass type: HeavyBox heavy = (HeavyBox) box;. You need it because the set of accessible members is decided by the reference type, not by the object. A downcast always compiles; if the object is not really a HeavyBox, it fails at run time with ClassCastException. Guard it with instanceof — and since Java 16 pattern matching does the test and the cast at once: if (box instanceof HeavyBox heavy) { ... }.
One exception to the reference-type rule: an overridden method is always dispatched on the actual object type. That is dynamic dispatch.
4. In what order do constructors run in an inheritance hierarchy, and why?
In what order do constructors run in an inheritance hierarchy, and why?
Constructors run top down — from the root of the hierarchy to the most derived class. For the chain E → F → G, calling new G() prints E, then F, then G.
The reason is that the compiler silently inserts a call to the no-argument superclass constructor as the first statement of every subclass constructor. A subclass builds its own state on top of the inherited state, so the superclass part must be initialised first.
Practical consequence: if the superclass has no no-argument constructor, there is nothing for that implicit call to invoke, so every subclass constructor must call the parent constructor with arguments explicitly, or the code will not compile.
5. Why does Java have no multiple inheritance of classes, and what replaces it?
Why does Java have no multiple inheritance of classes, and what replaces it?
A Java class extends exactly one superclass: class Shipment extends HeavyBox, ColorBox {} does not compile. The reason is the diamond problem — if two superclasses declared a member with the same name, the compiler could not decide which version the subclass inherits.
Java forbids multiple inheritance of state and implementation from classes, but allows multiple inheritance of types through interfaces: class Shipment extends HeavyBox implements Comparable<Shipment>, Serializable. After extends there is exactly one class; after implements there can be as many interfaces as you like.
Since Java 8 interfaces may carry default methods, so implementations can collide again — but the compiler refuses to guess. If two interfaces supply the same default method, the class must override it and, if needed, pick a version with InterfaceName.super.method().
6. How do you prevent a class from being inherited in Java: final and sealed
How do you prevent a class from being inherited in Java: final and sealed
Java gives three levels of control:
final class — the class cannot be extended at all. String, Integer and LocalDate are declared this way, which guarantees that nobody can break the invariants of an immutable type.
final method — the class stays extensible, but that particular method cannot be overridden.
sealed class (Java 17) — the author lists exactly which classes may extend it: public sealed class Box permits ColorBox, HeavyBox {}. Every permitted subclass must itself be declared final, sealed or non-sealed. Because the compiler knows the complete hierarchy, a switch over the type can be checked for exhaustiveness.