You Don't know JAVA #EP4 Part 1

You Don't know JAVA #EP4 Part 1

class Demo{
    public void call(Object obj){
        System.out.println("OBJECT");
    }
    public void call(String str){
        System.out.print("STRING");
    }
}

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

### GUESS THE ANSWER

1. OBJECT

2. STRING

3.Compile Time Error

4.Run Time Error

Answer: STRING

Let's Understand the problem now.

In Demo class there is an overloading method call(Object) , call(String). This is valid because both method signature are different. But if we call them in our main function like this

Case 1:

call(new Object())

call ("String")

Answer would be

OBJECT

STRING

Because we are passing different parameter. But our problem is Case 2:

Case 2:

call(null)

call (null)

This compiler can call any method Object one or String one but he will call String one.

Reason: In Between Object class and String class Object is Parent and String is Child. So the problem can be handle by child only it should not concern Object class. That's why String parameter method is calling.

Real Life Example:

If you have a problem in our company you won't complain directly to the CEO(Object) right you will first talk to your MANAGER(String). If manager can handle it . It should not concern The CEO. Hope you got it now.

Answer :

String

String

THIS IS PART 1 OF EP4 We will talk about Case 3 Scenario in NEXT BLOG. DO FOLLOW . THANK YOU FOR READING.

Did you find this article valuable?

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