Inheritance and Polymorphism Flashcards
6 cards from real AP CSA practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 6 Inheritance and Polymorphism flashcards as text
What is dynamic dispatch (dynamic method lookup) in Java?
Answer: The JVM determines which overridden method to call based on the actual runtime type of the object
Dynamic dispatch means the JVM uses the actual object type at runtime (not the declared reference type) to decide which overridden method to invoke.
If class Dog extends Animal and both have a `speak()` method, what does `Animal a = new Dog(); a.speak();` call?
Answer: Dog's speak() method
Due to dynamic dispatch, the JVM looks at the actual object type (Dog) at runtime and calls Dog's speak() method, not Animal's.
What does it mean for a class to be `abstract`?
Answer: It cannot be instantiated and may contain abstract methods
An abstract class cannot be instantiated directly and can declare abstract methods that subclasses must implement.
What must a concrete (non-abstract) subclass do with abstract methods it inherits?
Answer: Implement (override) all of them
A concrete subclass must provide implementations for all abstract methods inherited from its abstract superclass, or itself be declared abstract.
Can a subclass inherit from multiple superclasses in Java?
Answer: No, Java only supports single inheritance for classes
Java supports single class inheritance only — a class can extend only one superclass, though it can implement multiple interfaces.
What is a common use of the `instanceof` operator in polymorphism?
Answer: To check the actual runtime type of an object before a downcast
`instanceof` tests whether an object is an instance of a specific class, commonly used before casting a superclass reference to a subclass type.