mogli
mogli

Reputation: 1609

Why Object class methods are available in interface?

Following interface and classes are successfully compiled. Problem is mentioned in the output below :

interface MyInterface{}

class MyClass implements MyInterface{}

class InterDoubt{

    static MyInterface mi ;//= new MyClass() ;

    public static void main(String[] args){
        System.out.println("X") ;

        try{
            synchronized(mi){
                try{
                    mi.wait(4000) ;
                }
                catch(InterruptedException ie){
                    System.out.println("Exception occured at main.") ;
                }
            }
        }
        catch(Exception e){
            System.out.println("voilla, MyInterface is an interface,\n" + 
                       "then why compiler allows compilation of\n" +
                       "mi.getClass(), mi.wait().\n" +
                       "Or how the methods of Object class are available in an interface."
            );
        }

        System.out.println("Y") ;
    }
}

output :

X

voilla, MyInterface is an interface,

then why compiler allows compilation of

mi.getClass(), mi.wait().

Or how the methods of Object class are available in an interface.

Y


Edited :- I am accepting answer from disown, as it's the most explanatory. But after reading the answer, one more issue get's populated :-

"Remember if the interface tries to declare a public instance method declared 'final' in the Object class then it'll result into a compile-time error. For example, 'public final Class getClass()' is a public instance method declared 'final' in the Object class and therefore if an interface tries to declare a method with this signature then the compilation will fail" (Quoted from explanation).

then why the following code is getting successfully compiled :-

interface MyInter{
    public void method() ;
}

class MyClass implements MyInter{

    public final void method() {
        .......
        .......
              .......
    }

}

Upvotes: 2

Views: 2177

Answers (5)

dhananjay kumar
dhananjay kumar

Reputation: 1

The Java Language Specification clearly says that the members of an interface are those which are declared in the interface and those which are inherited from direct super interfaces. If an interface has no direct superinterface then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in the Object class, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by that interface.

Upvotes: 0

Joshua
Joshua

Reputation: 43298

The cast (Object)mi will always succeed so why should you be required to provide it?

Upvotes: 0

Alexander Torstling
Alexander Torstling

Reputation: 18898

What you are correctly pointing out as an exception is specified in the Java Language Specification. Interfaces will automatically get all members from the class java.lang.Object added. From here:

The Java Language Specification clearly says that the members of an interface are those which are declared in the interface and those which are inherited from direct super interfaces. If an interface has no direct superinterface then the interface implicitly declares a public abstract member method corresponding to each public instance method declared in the Object class, unless a method with the same signature, same return type, and a compatible throws clause is explicitly declared by that interface. This is what makes the signatures of the Object methods available to the compiler and the code compiles without any error. Remember if the interface tries to declare a public instance method declared 'final' in the Object class then it'll result into a compile-time error. For example, 'public final Class getClass()' is a public instance method declared 'final' in the Object class and therefore if an interface tries to declare a method with this signature then the compilation will fail.

Upvotes: 5

NawaMan
NawaMan

Reputation: 25687

Yes, all Object's methods are available to everything but Primitive value. Interface objects are still objects so they have Object's methods.

Upvotes: 0

Preet Sangha
Preet Sangha

Reputation: 65516

At run time there should be a real object (or null) behind the reference mi. The real type will implement this interface hence the compiler allows it. At run time any type that implements that interface could be there.

Upvotes: 2

Related Questions