Explore key differences and core principles between method overloading and method overriding in object-oriented programming. This quiz helps you clarify common confusions with practical questions and typical use case scenarios.
Which statement best describes method overloading in object-oriented programming?
Explanation: Method overloading involves creating multiple methods within the same class that share the same name but differ in the number or type of parameters. It allows flexibility within a single class when performing similar operations with different input types. The second option refers to overriding, where a subclass redefines a method from its parent with the same signature. The third option describes unrelated method use, as static and instance methods are not linked in this way. The fourth option incorrectly conflates access modifier changes with overriding.
If a subclass in an inheritance hierarchy provides a method with the same name, return type, and parameter list as one in its superclass, what concept is being demonstrated?
Explanation: When a subclass provides a method with the same signature (name, return type, and parameter list) as its superclass, it is overriding that method. This enables polymorphic behavior and allows the subclass to modify or extend the base class's functionality. Method overloading would involve different parameter lists. Shadowing involves variable name re-use but not method signature matches. Encapsulation refers broadly to hiding internal details, not to method signature matches.
Which characteristic differentiates method overloading from method overriding regarding when decisions are made?
Explanation: Method overloading resolution occurs at compile-time since the compiler selects the appropriate method based on the argument types. Conversely, method overriding involves dynamic dispatch, so the method to run is determined at runtime. The first option incorrectly swaps the timing for each. The third and fourth options neglect the important distinction in resolution timing between the two concepts.
In method overriding, which of the following is required regarding the access level of the overriding method in the subclass?
Explanation: For successful overriding, the overriding method must have either the same or a less restrictive (more accessible) access level than the method in the superclass. This ensures that subclass implementations remain at least as accessible as the parent. The first option is incorrect: more restrictive access would block calls where the parent allowed them. The second and fourth options are incorrect because both access limitations and consistency are essential in overriding.
What happens when a static method is redefined in a subclass using the same name and parameter list as in its superclass?
Explanation: Static methods in object-oriented programming are hidden, not overridden, when redefined in a subclass with the same signature. This is called method hiding or shadowing because static methods are resolved by class, not instance. Overriding applies only to instance methods. Overloading involves different parameter lists, not identical ones. The fourth option is incorrect; the language allows static methods with the same signature in subclasses, but the effect is hiding, not error.