Reputation: 1663
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?
Upvotes: 0
Views: 802
Reputation: 23788
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());
}
}
Upvotes: 2
Reputation: 2578
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.
Upvotes: 3
Reputation: 3514
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
Upvotes: 8