Reputation: 11
I am trying to execute the following code to clear the concept of method binding at Runtime.I have three classes Employee.java, Programmer.java,and JPolymorphism.java. I am trying to print the methods from employee and Programmer class.In method compilation is at runtime. By this way Programmer should be printed by the programmer.name but its printing Employee
Employee.java
package jpolymorphism;
class Employee {
String name = "Employee";
void printName() {
System.out.println(name);
}
}
Programmer.java
package jpolymorphism;
class Programmer extends Employee {
String name = "Programmer";
void print() {
System.out.println(name);
}
}
Test Clas
package jpolymorphism;
class JPolymorphism {
public static void main(String[] args) {
// TODO code application logic here
Employee emp = new Employee();
System.out.println(emp.name);
Employee programmer = new Programmer();
System.out.println(programmer.name);
emp.printName();
//The below code line shoul print Prograamer at run time
//but my code is prininting Employee Instead of Programmer
programmer.printName();
}
}
I am unable to understand Where is the error? As methods are binding at runtime and my last line of code programmer.name is of Programmer type and referance type is of employee type then according to the concept the line should pring Prograamer but it is also printing Employee .Where am i doing the mistake?
Upvotes: 0
Views: 227
Reputation: 291
The problem with the code is that when you are calling programmer.printName();
, it is calling the printName()
function of the Employee
class for which the variable is name
whose value is "Employee".
The thing is that even though Programmer
is a subclass of Employee
, the variable name
of the Programmer
class is not accessible to Employee
class. In inheritance, properties are inherited from the superclass, in this case, Employee
but Employee
cannot access the fields of its subclasses directly. So, when the programmer.printName();
invocation is done, the printName()
method of the Employee
class gets called and the value "Employee" gets printed.
As such the code does not reflect the concept of method binding at runtime as there is no override of a function as pointed out in the other answer.
Upvotes: 0
Reputation: 159086
Employee emp = new Employee();
System.out.println(emp.name);
That prints Employee
, obviously.
Employee programmer = new Programmer();
System.out.println(programmer.name);
That prints Employee
, because field binding happens at compile-time, so programmer.name
refers to the Employee.name
field, since programmer
is declared as Employee
.
emp.printName();
That prints Employee
, obviously.
programmer.printName();
That prints Employee
, because you're calling the Employee.printName()
method, which accesses the Employee.name
field, and the method has not been overridden by subclass Programmer
.
Why wasn't it overridden? Because the Programmer
method is named print
, not printName
. If you had added the @Override
annotation, as you should if you intended the method to override a superclass method, then the compiler would have told you that you had a problem.
If you rename the Programmer
method to printName
:
A good IDE would tell you to add the @Override
annotation.
The programmer.printName()
call will print Programmer
.
Upvotes: 2