How does a class consider its super class's method as it's interface method's implementation?

Instead of talking theory, let me directly jump to the example to demonstrate what I'm trying to ask.

// The below piece of code is present in a single java file named "MyMain.java"
public class MyMain {
    public static void main(String args[]) {
        IFace iFace = new CSub();
        iFace.method(); // This method essentially belongs to CSuper which doesn't implement IFace
    }
}

interface IFace {
    void method(); // Method in the interface
}

class CSuper {
    public void method(){ // some method in this class, but not the one which implements the method of IFace
        System.out.println("I'm not implementing IFace's method");
    }
}

class CSub extends CSuper implements IFace {} // No method implemented in the class and yet no error.

Obviously the above piece of code works as I was able to get the output I'm not implementing IFace's method upon execution.

My question is, how does CSub take the method() of CSUper as the implementation of the method() of IFace interface. It seems like inheritance is the one behind this, but I need some concrete answer for that. And also, is there a JLS reference for this which can clarify how this is possible via inheritance?


And a follow up question for that is, say there is some reason why this works(As I'm guessing that it is inheritance but need to be sure about it), why doesn't the same code snippet work if I do either of the below changes?

Change 1:

class CSuper {
    void method(){ // there is no access specifier, thus default(package)
        System.out.println("I'm not implementing IFace's method");
    }
}

Change 2:

class CSuper {
    protected void method(){ // access specifier is now protected
        System.out.println("I'm not implementing IFace's method");
    }
}

For both the above changes I do, I get an compilation error saying The inherited method CSUper.method() canoot hide the public abstract method in IFace on this line

class CSub extends CSuper implements IFace {}

Why is it so? Since it is inheritance, both protected and default access specifier should work, as all the classes are present in the very same file. And method() must have been inherited to the CSub class, just like how it did in the part one of the question. Can anybody highlight on this case too?

Jon Skeet
people
quotationmark

Tim's answered most of this. For the JLS reference, from section 8.1.5:

Unless the class being declared is abstract, the declarations of all the method members of each direct superinterface must be implemented either by a declaration in this class or by an existing method declaration inherited from the direct superclass, because a class that is not abstract is not permitted to have abstract methods (ยง8.1.1.1).

people

See more on this question at Stackoverflow