Method over-riding can be prevented by using final as a modifier at ______.

2016

Method over-riding can be prevented by using final as a modifier at ______.

  1. A.

    the start of the class. 

  2. B.

    the start of method declaration.

  3. C.

    the start of derived class.

  4. D.

    the start of the method declaration in the derived class.

Attempted by 331 students.

Show answer & explanation

Correct answer: B

Answer: Place the final modifier at the start of the method declaration in the class whose method you want to prevent from being overridden.

Explanation: Declaring a method with the final modifier prevents subclasses from providing an overriding implementation of that specific method.

Example (Java): class Base { final void display() { /* implementation */ } }

  • Declaring the method final in the original class prevents any subclass from overriding that method.

  • Declaring a class final prevents subclassing entirely, which also prevents overriding only because no subclasses can exist.

  • Making an overriding method final in a derived class only prevents further subclasses from overriding that derived-class method; it does not prevent the derived class from overriding the original method.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor