1Z0-811 Inheritance and Polymorphism 2 — Questions and Answers
Question 1: What is method overriding in Java?
- Defining a method with the same name but different parameters in the same class
- Providing a new implementation of a parent class method in a subclass (Correct answer)
- Calling a parent method using the super keyword
- Hiding a static method from a parent class
Correct 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.
Question 2: Which annotation should be used when overriding a method to catch signature errors at compile time?
- @Inherited
- @Super
- @Override (Correct answer)
- @Overwrite
Correct 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.
Question 3: Can a private method in a parent class be overridden by a subclass?
- Yes, if the subclass is in the same package
- Yes, always
- No, private methods are not inherited and cannot be overridden (Correct answer)
- No, unless the subclass uses the super keyword
Correct 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.
Question 4: What is the rule regarding access modifiers when overriding a method?
- The overriding method must have the exact same access modifier
- The overriding method can have a more restrictive access modifier
- The overriding method must have the same or broader (less restrictive) access modifier (Correct answer)
- The overriding method can freely change the access modifier
Correct 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.
Question 5: What is runtime polymorphism in Java?
- Method overloading resolved at compile time
- Method overriding resolved at runtime based on the actual object type (Correct answer)
- Using generics to handle multiple types
- Casting one reference type to another at runtime
Correct 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.
Question 6: Can a method declared 'final' in a parent class be overridden by a subclass?
- Yes, in any subclass
- Yes, only if the subclass is in the same package
- No, final methods cannot be overridden (Correct answer)
- No, unless the @Override annotation is applied
Correct answer: No, final methods cannot be overridden
A method declared with the 'final' keyword cannot be overridden by any subclass.
Question 7: Given Animal ref = new Dog(); where Dog extends Animal and both define speak(), which speak() is called on ref.speak()?
- Animal's speak() because the reference type is Animal
- Dog's speak() because the actual object is Dog (Correct answer)
- Both speak() methods are called sequentially
- A compilation error occurs
Correct 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.
What is method overriding in Java?