aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Does an interface extend Object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Does an interface extend Object?" Watch "Does an interface extend Object?" New topic
Author

Does an interface extend Object?

Alan Heebson
Greenhorn

Joined: Nov 05, 2007
Posts: 5
I think I remember reading somewhere that an Interface does not have a constructor. Does this mean that interfaces do not extend from Object?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11406
    
  16

as far as i know, an interface is not an object, so it does not extend Object.

an interface is nothing more than a contract that states what methods must exist in an object. you don't create something that is an interface, you create an object of some kind that meets the requirements of the interface.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Alan Heebson
Greenhorn

Joined: Nov 05, 2007
Posts: 5
Thanks Fred
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

You might then wonder how you can upcast a reference to an interface type, and still use that reference to call methods defined in Object.

The answer is in JLS 9.2 - Interface Members, which explains that an interface having no direct superinterfaces implicitly declares a public abstract method for each public instance method in Object.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Niranjan Welankiwar
Greenhorn

Joined: Mar 28, 2012
Posts: 4
If interface does not extends Object class then can please someone explain me the below code :

1.
interface a{

@Override
public int hashCode();

@Override
public String toString();

}


In above case, "a" interface is the new interface declared. Yet, no class is implementing it. Then how come the methods from Objects class methods are visible in interface ?

2.

If it doesn't extends Object class, then why I can't declare below method in interface ?

public Class getClass();

When I tried to declare this method in interface, it says cannot override from Object class as it is declared as final method in Object class.

Got confused . Please someone explain.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Niranjan Welankiwar wrote:If interface does not extends Object class then can please someone explain me the below code :
In above case, "a" interface is the new interface declared. Yet, no class is implementing it. Then how come the methods from Objects class methods are visible in interface ?
...
When I tried to declare this method in interface, it says cannot override from Object class as it is declared as final method in Object class.


Read Marc Weber's post from 2007, right above yours.
Helen Ma
Ranch Hand

Joined: Nov 01, 2011
Posts: 451
Hi, according to KB, you can see interface as a 100% abstract class. An abstract class has at least one method declaration without any implementation. For interface, it must have all methods declaration without any implementation.
So, if an abstract class extends Object, an interface can also overrides the toString / equals/ hashCode methods from Objects.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
Helen Ma wrote:An abstract class has at least one method declaration without any implementation.

No it doesn't. You can have an abstract class with no abstract methods.

Helen Ma wrote:So, if an abstract class extends Object, an interface can also overrides the toString / equals/ hashCode methods from Objects.

Despite what it says in KB, a 100% abstract class and an interface are not the same thing.
As Marc said above, interfaces that don't extend another interface implicitly include abstract definitions of all the public methods in the Object class.


Joanne
Yalvin Duha
Ranch Hand

Joined: Apr 07, 2012
Posts: 40

Niranjan Welankiwar wrote:If interface does not extends Object class then can please someone explain me the below code :

1.
interface a{

@Override
public int hashCode();

@Override
public String toString();

}

In above case, "a" interface is the new interface declared. Yet, no class is implementing it. Then how come the methods from Objects class methods are visible in interface ?

2. If it doesn't extends Object class, then why I can't declare below method in interface ?

public Class getClass();

When I tried to declare this method in interface, it says cannot override from Object class as it is declared as final method in Object class.


I'm glad I read some of these posts, because there are nitpicks that personally I've never thought about before. And generally, it forces you to dig a bit deeper, and that's great for the exam.

An interface does not extend anything; it's not a class and therefore it does not implicitly extend Object. Nonetheless, every other class does which perhaps prompted the language specification writers to take into account situations where the user explicitly creates an interface that includes Object's method, presumably to force the interface type (implementation) to implement them, which by in itself is unnecessary, because after all, every class is an Object, and therefore inherits all those methods automatically.

So, for such scenarios, as it was mentioned before, the spec people thought it's better to catch any misgiving at the compile-time. This is an excerpt from the Java Language Specification 7 on interface members (http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.2):

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.

It is a compile-time error if the interface explicitly declares such a method m in the case where m is declared to be final in Object.

It follows that is a compile-time error if the interface declares a method with a signature that is override-equivalent (ยง8.4.2) to a public method of Object, but has a different return type or incompatible throws clause.


There are three types of visibility and accessibility designated to methods in Object class: non-final public, final, and protected.

  • For obviously reasons, final methods, i.e. wait()'s, notify()'s, and getClass(), cannot be implemented by a class that implements such interface. Consequently, the compiler must prevent the code from declaring such methods in the interface.
  • The non-final public methods can be declared in the interface with the proper preservation of overriding rules. They do not, however, need to be implemented by any subtype class, because, again, every class is implicitly an Object.
  • Finally, we have protected methods, i.e. clone() and finalize(). If you declare such methods in your interface, it becomes implicitly public abstract static. However, in the implementing class, since the overridden methods declared in the interface are "public", you've narrowed the accessibility from Object's protected to YourInterface's public, therefore you're obligated to keep visibility at public level in your implementing class as well. Consequently, you must define these two methods, i.e. clone() and finalize(), explicitly with a public modifier.



  • saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Supplement


    Tell the difficulties that i am difficult.
    gurpeet singh
    Ranch Hand

    Joined: Apr 04, 2012
    Posts: 924
        
        1

    very nice explanation yalvin
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Does an interface extend Object?