File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes java.lang.Object is a class or Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java.lang.Object is a class or Interface" Watch "java.lang.Object is a class or Interface" New topic
Author

java.lang.Object is a class or Interface

nalamati satyanarayana
Ranch Hand

Joined: Dec 16, 2005
Posts: 36
Hi All,
java.lang.Object is a class or Interface
Here my question is if it is class for example I was created the interface like

In test main class through the reference variable i was able to access the Object class method like

As per the java standard interface cannot extend the class but here I am able to access Object class method.


John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Object class is the root - even interfaces extend them... Think it like an exception...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

As you can see in the API documentation, java.lang.Object is a class.

Have you tried running your program? You'll get a NullPointerException in line 10 when you try to run it, because i is null.

You can't directly create an instance of an interface. If you would try MyInterface i = new MyInterface(); you would get a compile-time error. You can, however, instantiate a class that implements the interface, and assign it to a variable of the interface type:


John Jai wrote:Object class is the root - even interfaces extend them... Think it like an exception...

That's not really true. Interfaces do not extend class Object. But whenever you have an object, it is always an instance of some class, and classes always extend Object. So the methods of class Object are available on any object.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

nalamati satyanarayana wrote:
As per the java standard interface cannot extend the class but here I am able to access Object class method.

An interface cannot exist on it's own; every "instance" of an interface must be a class that implements that interface. All classes extend Object, and thus, any implementation of an interface will be a subclass of Object.

"The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." - George Bernard Shaw
nalamati satyanarayana
Ranch Hand

Joined: Dec 16, 2005
Posts: 36
Jesper,
Interfaces can extend the class?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

No, an interface cannot extend a class.

But a class can ofcourse implement an interface.
Riaan Nel
Ranch Hand

Joined: Apr 23, 2009
Posts: 157

nalamati satyanarayana wrote:Jesper,
Interfaces can extend the class?

No, it's the other way around, classes can implement interfaces - that's it. Try to compile an interface that extends a class, and see what the compiler says.
nalamati satyanarayana
Ranch Hand

Joined: Dec 16, 2005
Posts: 36
Object class is root class for all classes ?
But in my above code even it null reference it showing all the methods of Object class
here my question is with out extend how it is possible to see the methods of object class?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

The compiler does not look if the variable is null or not. This will be checked at runtime, not at compile time. When you run that code, then you'll get a NullPointerException.

The compiler only looks at the type of the variable i, not at its value. It sees that i is of a type that refers to an object, so then it knows that that the methods of class Object can be called on it.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

From the Java Language Specification:
9.2 Interface Members

The members of an interface are:

  • Those members declared in the interface.
  • Those members inherited from direct superinterfaces.
  • 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.

  • That third one explicitly says that interfaces "inherit" Object's methods.


    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    nalamati satyanarayana
    Ranch Hand

    Joined: Dec 16, 2005
    Posts: 36
    Thanks for giving the clear details ,now I have got clear picture
    John Jai
    Bartender

    Joined: May 31, 2011
    Posts: 1776
    Jesper de Jong wrote:
    John Jai wrote:Object class is the root - even interfaces extend them... Think it like an exception...

    That's not really true. Interfaces do not extend class Object. But whenever you have an object, it is always an instance of some class, and classes always extend Object. So the methods of class Object are available on any object.

    Thanks.. I got it...
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: java.lang.Object is a class or Interface
     
    Similar Threads
    method unavailable
    local inner classes
    anonymous class?
    What the??? Interface & Implements in regards to inheritance & polymorphisms
    diff between abstract class and inheritance