Astak Qes
Astak Qes

Reputation: 13

Preference between Interface and Abstract (or Super) class method

I have a question: In Java, I have a class Fresher, which extends Employee and implements an interface IFresher. Please refer the code below.

public interface IFresher {
        // Some other stuff
    public void takeCall();
}



public class Employee extends Human {
        // Some other stuff
    public abstract void takeCall()
       {
          // Some basic implementation
       }

}

class  Fresher extends Employee implements IFresher
{
        @Override
    public void takeCall() {

    }
}

Question: Which takeCall() method gets implemented in the Fresher sub class, is it from interface or the super class, What is order of hierarchy followed in such cases when there is a conflict between the super class and the interface?

Upvotes: 1

Views: 792

Answers (2)

mikera
mikera

Reputation: 106351

There is never any conflict in this case - since interfaces don't contain implementations, there can only ever be one (superclass) implementation to inherit from.

Fresher will inherit implementation from Employee (in this case overriding the takeCall method). But it will also successfully implement IFresher as it has a function with the correct signature.

(Aside: this is the main reason why multiple implementation inheritance is disallowed in Java, it gets very tricky to define how it should all work)

Upvotes: 2

Edmon
Edmon

Reputation: 4872

In your case, Fresher takes precedence, specially since you are explicitly overriding.

Interfaces are not taken into account as they only provide contract for the function signature and no functionality.

Upvotes: 0

Related Questions