0
0
Javaprogramming~5 mins

Method overriding in Java - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is method overriding in Java?
Method overriding occurs when a subclass provides its own version of a method that is already defined in its superclass. It allows the subclass to change or extend the behavior of that method.
Click to reveal answer
intermediate
What are the rules for method overriding in Java?
The method in the subclass must have the same name, return type, and parameters as the method in the superclass. The overriding method cannot have more restrictive access than the overridden method.
Click to reveal answer
beginner
Can a private method be overridden in Java?
No, private methods cannot be overridden because they are not visible to subclasses. They are only accessible within the class they are declared in.
Click to reveal answer
beginner
What is the use of the @Override annotation?
The @Override annotation tells the compiler that the method is intended to override a method in the superclass. It helps catch errors if the method does not correctly override any method.
Click to reveal answer
intermediate
What happens if you call an overridden method on a superclass reference pointing to a subclass object?
The subclass's version of the method is called. This is called dynamic method dispatch and supports runtime polymorphism.
Click to reveal answer
Which of the following is true about method overriding?
AThe subclass method must have the same signature as the superclass method.
BThe subclass method can have a different return type than the superclass method.
CPrivate methods can be overridden.
DThe subclass method must have more restrictive access than the superclass method.
What does the @Override annotation do?
AIt forces the method to be private.
BIt changes the method's return type.
CIt prevents the method from being overridden.
DIt tells the compiler the method is intended to override a superclass method.
If a superclass reference points to a subclass object, which method is called when invoking an overridden method?
AThe subclass method.
BBoth methods are called.
CThe superclass method.
DAn error occurs.
Can a subclass override a static method from its superclass?
AYes, static methods can be overridden.
BOnly if the superclass method is public.
CNo, static methods cannot be overridden but can be hidden.
DOnly if the subclass method is also static.
Which access modifier is NOT allowed for an overriding method compared to the overridden method?
ASame restrictive
BMore restrictive
CLess restrictive
DPublic
Explain method overriding and why it is useful in Java.
Think about how subclasses can change what a method does.
You got /4 concepts.
    Describe the rules and restrictions when overriding a method in Java.
    Focus on what must stay the same and what cannot be changed.
    You got /4 concepts.