0

Firstly, I though that java's Polymorphism functions are mapped by it types of parameter instance.

Please, someone help to explain why my function haven't called to myFunction(EmployeeImpl emp) sign it instance is EmployeeImpl.

public class MainApp {
  public static void main(String[] args){
    Employee emp = new EmployeeImpl();
    emp.callMyFunction();
  }
}

abstract class Employee{
  public void callMyFunction(){
    //here is huge amount of code, which all child class has the same
    //excepted this line is called to a different function by it instant types.
    EmployeeFacade.myFunction(this);
  }
}
class EmployeeImpl extends Employee{

}
class EmployeeFacade{
  public static void myFunction(Employee emp){
    //same data to database
    System.out.println("Employee: "+ emp.getClass().getName());
  }
  public static void myFunction(EmployeeImpl emp){
    //same data to database
    System.out.println("EmployeeImpl: "+ emp.getClass().getName());
  }
}

Result: Employee: EmployeeImpl

Edited: This is just a sample application with the same structure as my reality application, which has more than 20 children classes that contain the same function called callMyFunction, this function has more than 20 lines of code. so it's a very hard work for me to override this function with the same code code for all children class. Anyways, What will happen if I need to change my function on the future? Would I change all 20 function with the same code?

Are there anyways easier than this?

Se Song
  • 1,613
  • 2
  • 19
  • 32
  • 2
    Because you're not overriding `callMyFunction` in the `EmployeeImpl`. When you call an overloaded method, it is determined at compile-time which method will be used. So no matter how this class is inherited at runtime. – align Mar 27 '17 at 04:23
  • Editing comment because @align explained the behavior you saw a lot better than my comment. I will add this comment though: Look at what would happen if in main() you did EmployeeImpl emp = new EmployeeImpl(); instead – muzzlator Mar 27 '17 at 04:24
  • So, Must I override my function? what about if myfunction is very hug amount of code. Must I rewrite it again on my subclass? – Se Song Mar 27 '17 at 04:32
  • @SeSong, I think providing more details on what you are really trying to achieve is good way to get better answers. For example, why all those `myFunction` methods are moved to `EmployeeFacade` class as static methods instead of being virtual methods of the `Employee` class itself? – SergGr Mar 31 '17 at 05:42

3 Answers3

8

Don't exist dynamic binding for overloaded methods ...

Java uses static binding for overloaded methods, and dynamic binding for overridden ones.

Java dynamic binding and method overriding

Community
  • 1
  • 1
Yashar Panahi
  • 2,816
  • 1
  • 16
  • 22
3

There are 2 type of Polymorphism

1)Static polymorphism

2)Dynamic polymorphism

your case is static polymorphism

If you debug your code it's always called

public static void myFunction(Employee emp){
  System.out.println("Employee: "+ emp.getClass().getName());
}

and every class having getClass() method and it's return the runtime class of the object which has method called. Here is JDK implementation of Object class

 public final native Class<?> getClass();

and it's Class class implementation

    public String getName() {
    String name = this.name;
    if (name == null)
        this.name = name = getName0();
    return name;
}

Returns the name of the entity (class, interface, array class, primitive type, or void) represented by this Class object, as a String.

Yasir Shabbir Choudhary
  • 2,458
  • 2
  • 27
  • 31
  • Are there anyways to make it dynamic, because I have more than 20 children classes and hug amount of code inside `callMyFunction`. Anyways facade function are do different jobs. – Se Song Mar 28 '17 at 01:22
2

My first solution (as I suggested in comments) would be to move myFunction from the EmployeeFacade to Employee, EmployeeImpl and other subclasses and thus use virtual methods directly. If this for some reasons is not an option, my next solution would be introducing virtual "proxy" function to Employee and using it to dispatch call properly:

public class MainApp {
  public static void main(String[] args){
    Employee emp = new EmployeeImpl();
    emp.callMyFunction();
  }
}

abstract class Employee
{
    public void callMyFunction()
    {
        //here is huge amount of code, which all child class has the same
        //excepted this line is called to a different function by it instant types.
        callMyFunctionImpl();
    }

    protected void callMyFunctionImpl()
    {
        EmployeeFacade.myFunction(this);
    }
}

class EmployeeImpl extends Employee
{
    @Override
    protected void callMyFunctionImpl()
    {
        EmployeeFacade.myFunction(this);
    }
}

class EmployeeFacade
{
    public static void myFunction(Employee emp)
    {
        //same data to database
        System.out.println("Employee: " + emp.getClass().getName());
    }

    public static void myFunction(EmployeeImpl emp)
    {
        //same data to database
        System.out.println("EmployeeImpl: " + emp.getClass().getName());
    }
}
SergGr
  • 23,570
  • 2
  • 30
  • 51