Manish Patil
Manish Patil

Reputation: 29

Understanding Overriding Concept

I have two classes, Employee and Department.

Department is subclass of Employee.

The code is 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 am running following code.

 public static void main(String[] args) {
        Employee e1 = new Department();
        e1.getEmployeeDetails("Manish", "10");
        e1.print();
    }

The output is

Employee.getEmployeeDetails()

Department.print()

When I run the getEmployeeDetails() method the JVM is calls the parent class, but when I call the print() method the JVM calls the child class.

Why is this happening this way?

Upvotes: 2

Views: 110

Answers (5)

Shubhendu Pramanik
Shubhendu Pramanik

Reputation: 2751

For getEmployeeDetails method is not getting overridden as its parameters are different. The rule of overriding is that the method signature must be the same which is not the case here.

Also you should read about dynamic method dispatch which feature tells that at runtime it's decided which method to be called(parent or child).

For example:

1) For below it will call method of Department class as the instance is of Department class even if the instance variable is of Employee class:

Employee e1 = new Department();
e1.getEmployeeDetails("Manish", "10");
e1.print();

2) In below case it will call the method of Employee class:

Employee e1 = new Employee();
e1.getEmployeeDetails("Manish", "10");
e1.print();

Hope this clears your doubt ;)

Upvotes: 0

Mark Adelsberger
Mark Adelsberger

Reputation: 45819

If you want to understand the overriding concept, as your question is titled, then you need to understand inheritance. This example (creating an inheritance relationship between Employee and Department) does not reflect such an understanding, so really you need to take a step back.

Something like Manager could be a subclass of Employee, because every manager is an employee.

Department could be subclass of something like OrgUnit because every department is an organizational unit.

But a department is not an employee, so there is no conceptual sense to using extends in the way you've shown; so at a conceptual level, this example cannot be used to explain overriding.

To understand the mechanism of overriding, this example is usable. In that context, the reason for the behavior you see is that a subclass method only overrides a superclass method if it accepts the same argument types. What you have with getEmployeeDetails() is method overloading - two methods with the same name but incompatible argument lists

Upvotes: 0

Frank
Frank

Reputation: 881

Override a method should respect signature ,it means method name and parameters type should be the same. Note: the type you are returning doesn't effect it

In your child class you are not overriding the parent method .

Upvotes: 0

jlaitio
jlaitio

Reputation: 1948

Your method signatures for the getEmployeeDetails methods differ. Since you are supplying two strings as parameters, the superclass method is called since the Department class does not have a method with such a signature (it only has one with a String and int parameter).

The print method is correctly Overriden since it has the same method signature (method name, parameters and their types). Hence it is called on the Department class.

Upvotes: 0

Eran
Eran

Reputation: 394146

getEmployeeDetails() of Department doesn't override getEmployeeDetails() of Employee, since it has different type of arguments (one takes 2 Strings and the other takes a String and an int). That's why e1.getEmployeeDetails("Manish", "10"); calls the method of the base class Employee, which takes 2 Strings.

On the other hand, print() of Department does override print() of Employee, since it has the same signature. Therefore print() of Department is executed.

Upvotes: 4

Related Questions