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 keyword does a subclass use to inherit from a superclass in Java?
Answer: extends
In Java, a subclass uses the `extends` keyword to inherit fields and methods from its superclass.
What does it mean to override a method in Java?
Answer: A subclass provides its own implementation of a method defined in the superclass
Method overriding occurs when a subclass provides a specific implementation for a method already defined in its superclass.
What keyword is used to call the superclass constructor from a subclass constructor?
Answer: super()
The `super()` call in a subclass constructor explicitly invokes the superclass constructor to initialize inherited fields.
Which OOP principle allows a subclass object to be used wherever a superclass reference is expected?
Answer: Polymorphism
Polymorphism allows a subclass instance to be assigned to a superclass reference variable, enabling flexible and interchangeable use.
What is the difference between method overriding and method overloading?
Answer: Overriding redefines a superclass method; overloading defines multiple methods with the same name but different parameters
Overriding replaces a superclass method in a subclass with the same signature, while overloading creates multiple methods with the same name but different parameters in the same class.
What is the purpose of the `@Override` annotation in Java?
Answer: It tells the compiler to verify the method actually overrides a superclass method
`@Override` is a compile-time check that causes a compile error if the annotated method doesn't actually override a superclass method.