When a method in a subclass has the same name and type signatures as a method…

2016

When a method in a subclass has the same name and type signatures as a method in the superclass, then the method in the subclass _____ the method in the superclass.

  1. A.

    Overloads

  2. B.

    Friendships

  3. C.

    Inherits

  4. D.

    Overrides

Attempted by 347 students.

Show answer & explanation

Correct answer: D

Answer: Overrides — when a subclass provides a method with the same name and signature as a method in its superclass, the subclass method overrides the superclass method.

Key points:

  • Same method name and same parameter types (same signature) in the subclass and superclass.

  • Return type must be the same or covariant (language-dependent).

  • Access level cannot be more restrictive in the subclass than in the superclass.

  • Static methods are not overridden but hidden; final methods cannot be overridden.

  • Using an annotation like @Override (in Java) helps catch mistakes where you intended to override but signatures differ.

Difference from overloading: Overloading is the same method name with different parameter lists (different signatures); overriding uses the same signature and replaces the inherited implementation.

Example (Java-like):

  1. class Super { void show() { System.out.println('Super'); } }

  2. class Sub extends Super { @Override void show() { System.out.println('Sub'); } }

When show() is called on an instance of Sub, Sub's implementation runs — this is overriding (runtime polymorphism).

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

Explore the full course: Mppsc Assistant Professor