Which Overloaded Method is Called in Java

后端 未结 5 565
一向
一向 2021-01-30 12:56

I have a basic inheritance situation with an overloaded method in the super class.

public class Person {
    private String name;
    private int dob;
    priva         


        
5条回答
  •  抹茶落季
    2021-01-30 13:26

    The overload resolution happens during compile time, not at runtime.

    So, when you call getWorkDetails(this), this is assumed to be a Person (which is the static type) and hence called the corresponding overload.

    Note: Using this inside Employee class would have made it an Employee type. You can verify this by overloading work() in Employee like this.

    class Employee extends Person {
        ...
    
        public void work() {
            getWorkDetails(this); // This should print "This person is an Employee"
        }
    }
    

提交回复
热议问题