1

Interface Implicitly inherit the Object class. Because interface SubInterface default Method call the Objectclass hashCode Method. It's possible then How & Why..?

package com.oca.test.exam;

    interface SuperInterface {
        default void printStuff() { System.out.println("Default Method"); }
    }

    interface SubInterface extends SuperInterface {
        default void doStuff() {
            this.printStuff();
            System.out.println(this.hashCode());
        }
    }

    public class InterfaceAbstractCombination implements SubInterface{

        public static void main(String[] args) {
            SubInterface sub = new InterfaceAbstractCombination();
            sub.doStuff();
        }
    }
Ng Sharma
  • 2,072
  • 8
  • 27
  • 49
  • 1
    `this` is referring to the instance of `InterfaceAbstractCombination` class, not the interface itself. – Kartik Feb 25 '19 at 04:54
  • Possible duplicate of [why this keyword is used in java interface and what does it refer?](https://stackoverflow.com/questions/42780642/why-this-keyword-is-used-in-java-interface-and-what-does-it-refer) – Kartik Feb 25 '19 at 04:56
  • Possible duplicate of [Do interfaces inherit from Object class in java](https://stackoverflow.com/questions/6056124/do-interfaces-inherit-from-object-class-in-java) – ilinykhma Feb 25 '19 at 04:57

2 Answers2

2

The interface is not inheriting the Object clas. The class which implements the interface SubInterface is inheriting the Object class.

Just think about it, will you be able to call doStuff() of SubInterface directly? You need to implement that interface in another class, create an instance of that class then you can call doStuff().

So InterfaceAbstractCombination class implements the SubInterface and when you call doStuff() you calling it on the instance of InterfaceAbstractCombination which is supplying the this.hashCode() inherited from the Object class, so this will refer to the instance of the class implementing the interface.

One thing to note, If you check the JLS spec

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type r, and throws clause t corresponding to each public instance method m with signature s, return type r, and throws clause t declared in Object, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by the interface.

So this is why you are able to call SuperInterface.super.hashCode();.

enter image description here

Fullstack Guy
  • 16,368
  • 3
  • 29
  • 44
0

The hashCode of InterfaceAbstractCombination class is provided in your inheritance hierarchy. this does not belong to interface, even if it has default method.

Consider running below code you'll see same hashCode getting printed:

package com.oca.test.exam;

  public class InterfaceAbstractCombination implements SubInterface {

    public static void main(String[] args) {
        SubInterface sub = new InterfaceAbstractCombination();
        sub.doStuff();
        System.out.println(sub.hashCode());
    }
    }

    interface SuperInterface {
    default void printStuff() {
        System.out.println("Default Method");
    }
    }

    interface SubInterface extends SuperInterface {
    default void doStuff() {
        this.printStuff();
        System.out.println(this.hashCode());
    }
    }
Atul Dwivedi
  • 1,452
  • 16
  • 29