You Don't know JAVA #EP4 Part 2

You Don't know JAVA #EP4 Part 2

If you are reading this before Part 1. I recommend you to go through that blog because we are going to discuss case 3 scenario and case 1 and case 2 discussed on part 1.

Hope you are ready for Case 3: || #EP4 PART 2

CASE 3:

class Demo{
    public void call(String s){
        System.out.println("STRING");
    }
    public void call(StringBuffer str){
        System.out.print("STRING BUFFER");
    }
}

public class Main{
    public static void main(String [] args){
        Demo d= new Demo();
        d.call(null);
    }
}

GUESS THE ANSWER:

1. STRING

2. STRING BUFFER

3. Compile Time Error

4. Run Time Error

ANSWER: Compile Time Error

Error: error: reference to call is ambiguous

both method call(String) in Demo and method call(StringBuffer) in Demo match

Explanation: Yesterday's example when we are talking about Object and String. Where Object is Parent and String is Child. And String will get preference because of CEO(Object) and Manager(String). If your problem is solved by manager you don't need to go the CEO right.

But in case 3: Both String and StringBuffer have same priority/preference so in this case compiler will get confuse and raise a Compile Time Error.

Real Life Example Case 2:

If your mother have 1 chocolate and one older(Object) and younger(String) son your mom will give chocolate to the younger son. That's why String has preference

Real Life Example Case 3:

If your mother have 1 chocolate and both son are of same age. Your mom can't decide between you two. That's why Compile Time Error.

Did you find this article valuable?

Support Hritik manbattulwar by becoming a sponsor. Any amount is appreciated!