java - Understanding Overriding Concept -
i have 2 classes, employee
, department
.
department
subclass of employee
.
the code below:
public class employee { public void getemployeedetails(string name , string age) { system.out.println("employee.getemployeedetails()"); } public void print() { system.out.println("employee.print()"); } } public class department extends employee { public void getemployeedetails(string name , int age) { system.out.println("department.getemployeedetails()"); } public void print() { system.out.println("department.print()"); } }
i running following code.
public static void main(string[] args) { employee e1 = new department(); e1.getemployeedetails("manish", "10"); e1.print(); }
the output
employee.getemployeedetails() department.print()
when run getemployeedetails()
method jvm calls parent class, when call print()
method jvm calls child class.
why happening way?
getemployeedetails()
of department
doesn't override getemployeedetails()
of employee
, since has different type of arguments (one takes 2 string
s , other takes string
, int
). that's why e1.getemployeedetails("manish", "10");
calls method of base class employee
, takes 2 string
s.
on other hand, print()
of department
override print()
of employee
, since has same signature. therefore print()
of department
executed.
Comments
Post a Comment