Inheritance and Polymorphism Flashcards
7 cards from real 1Z0-811 practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Inheritance and Polymorphism flashcards as text
What is method overriding in Java?
Answer: Providing a new implementation of a parent class method in a subclass
Method overriding occurs when a subclass provides a specific implementation of a method already defined in its parent class with the same signature.
Which annotation should be used when overriding a method to catch signature errors at compile time?
Answer: @Override
The @Override annotation tells the compiler to verify that the method actually overrides a parent method, catching errors if the signature doesn't match.
Can a private method in a parent class be overridden by a subclass?
Answer: No, private methods are not inherited and cannot be overridden
Private methods are not visible to subclasses and therefore cannot be overridden; a subclass defining a same-name private method simply creates a new unrelated method.
What is the rule regarding access modifiers when overriding a method?
Answer: The overriding method must have the same or broader (less restrictive) access modifier
An overriding method must have the same or broader (less restrictive) access modifier than the method it overrides.
What is runtime polymorphism in Java?
Answer: Method overriding resolved at runtime based on the actual object type
Runtime polymorphism (dynamic dispatch) occurs when the JVM determines which overridden method to call based on the actual object type at runtime, not the reference type.
Can a method declared 'final' in a parent class be overridden by a subclass?
Answer: No, final methods cannot be overridden
A method declared with the 'final' keyword cannot be overridden by any subclass.
Given Animal ref = new Dog(); where Dog extends Animal and both define speak(), which speak() is called on ref.speak()?
Answer: Dog's speak() because the actual object is Dog
Due to dynamic dispatch, the JVM calls Dog's speak() because the actual object at runtime is of type Dog, regardless of the reference type.