Inheritance - Overriding Methods QuizS2C Home « Inheritance - Overriding Methods Quiz

OO Concepts Quiz 5

The quiz below tests your knowledge of the material learnt in OO Concepts - Lesson 5 - Overriding Methods.

Question 1 : What happens to the following code snippet at compile time?

public class A {
    public void a() {
    }
}
public class B extends A {
    public static void a() {
    }
}
- We can't <i>override</i> an instance method with a static method, the compiler gets upset.
Question 2 : Method overrides can't be less restrictive?
- Less restrictive is fine for method overrides, more restictive causes a compiler error.
Question 3 : What is the following an example of?

int i; // instance variable in superclass
int i; // instance variable in subclass
- Instance variables are not overridden in subclasses but when present with the same name this is known as <i>hiding</i>. Think of it as a redefinition.
Question 4 : Method overrides must have different argument lists?
- Method overrides must have the same argument lists or its an overloaded method.
Question 5 : Is the following a valid override?

void a() { ... } // a() method in superclass
public void a() { ... } // a() method in subclass
- The method override is less restrictive so it's fine.
Question 6 : We can override the instance variables of a superclass?
- We can only <i>override</i> the methods of a <i>superclass</i>.
Question 7 : Is the following a valid override?

private void a() { ... } // a() method in superclass
public void a() { ... } // a() method in subclass
- We CANNOT override private methods so the <code>a()</code> method in the <i>subclass</i> knows nothing of the <code>a()</code> method in the <i>superclass</i> and so this isn't an override.
Question 8 : When we override a method in a subclass and we invoke that method using an instance of the subclass, what gets called at runtime?
- The overridden method in the <i>subclass</i> will be called.
Quiz Progress Bar Please select an answer


What's Next?

In the next quiz we test your knowledge of inheritance concepts.