Mastering Java: The Ultimate Quiz for 'Thinking in Java'

Disable ads (and more) with a membership for a one time $2.99 payment

Mastering Java: The Ultimate Quiz for 'Thinking in Java'. Dive deep into Java with multiple-choice questions. Challenge yourself with quiz questions designed to test and improve your understanding of the 'Thinking in Java' book. Get ready for your exam!

Practice this question and more.


How does Java handle a method in a derived class that has the same signature as a private method in the base class?

  1. It overrides the base class method

  2. It overloads the base class method

  3. It is considered a new method not related to the base class method

  4. It triggers a compile-time error

The correct answer is: It overloads the base class method

Java handles methods with the same signature in a derived class as private methods in the base class by overloading the base class method. This means that the derived class will have a new method with the same name and same parameters, but it will have a different implementation from the private method in the base class. Option A is incorrect because if the method in the base class was public, then the derived class would override it. Option C is incorrect because even though the method has the same signature, it is still related to the method in the base class and can be accessed through inheritance. Option D is incorrect because it will not cause a compile-time error as it is allowed in Java to overload private methods from the base class in a derived class.