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 a 'has-a' relationship in OOP (as opposed to 'is-a')?
Answer: Composition — one class contains a reference to another class
A 'has-a' relationship is composition: a class holds a reference to another class as a field, rather than inheriting from it.
What does the `final` keyword do when applied to a method in Java?
Answer: Prevents the method from being overridden in subclasses
A `final` method cannot be overridden by any subclass, locking its implementation for the entire inheritance hierarchy.
Which of the following correctly performs a downcast from Animal to Dog?
Answer: Dog d = (Dog) a;
A downcast requires an explicit cast operator `(Dog)` to convert a superclass reference to a subclass reference.
What is the role of an interface in Java's type system for AP CSA purposes?
Answer: It defines a contract of methods that implementing classes must provide
A Java interface specifies a set of method signatures that any implementing class must define, establishing a contract without providing implementations.
If a subclass constructor does not explicitly call `super()`, what happens in Java?
Answer: Java automatically inserts a call to the no-argument superclass constructor
If no explicit super() call is made, Java implicitly calls the superclass's no-argument constructor as the first action of the subclass constructor.
What is the term for when a superclass method calls an overridden method that runs the subclass version?
Answer: Polymorphic dispatch
Polymorphic dispatch (dynamic binding) means that even when called from within a superclass method, the overridden subclass version runs because the object's actual type governs the call.