✏️ Explanatory Question

[Polymorphism]

In the given code snippet, which feature of object‑oriented programming concept is being exhibited by the substring() method?
String s = "sandwich"; System.out.println(s.substring(4)); System.out.println(s.substring(1,4));

👁 0 Views
📘 Detailed Answer
🟢 Easy
💡

Answer with Explanation

The feature exhibited is: Method Overloading (Compile-time Polymorphism)

Understanding the Code:

The String class provides multiple versions of the substring() method.

Two Different Method Calls:

s.substring(4) s.substring(1,4)

Meaning of Each:

Method Call Meaning
substring(int start) Returns substring from index to end
substring(int start, int end) Returns substring from start to end‑1

Output Explanation:

  • s.substring(4) → "wich"
  • s.substring(1,4) → "and"

What is Method Overloading?

Method overloading means:

  • Same method name
  • Different parameter list
substring(int start) substring(int start, int end)

Both methods have the same name but different parameters.

Type of Polymorphism:

Polymorphism Type Description
Compile-time Polymorphism Method overloading
Run-time Polymorphism Method overriding

Important Concept:

Same method name + different parameters → Method Overloading

Conclusion:

  • substring() is defined in multiple forms.
  • Java selects method based on parameters passed.
  • This is an example of compile-time polymorphism.
Correct Answer: Method Overloading