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?