• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.Object is a class or Interface

 
nalamati satyanarayana
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object class is the root - even interfaces extend them... Think it like an exception...
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
nalamati satyanarayana
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper,
Interfaces can extend the class?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, an interface cannot extend a class.

But a class can ofcourse implement an interface.
 
Riaan Nel
Ranch Hand
Posts: 160
IntelliJ IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15482
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20665
65
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
     
    nalamati satyanarayana
    Ranch Hand
    Posts: 39
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for giving the clear details ,now I have got clear picture
     
    John Jai
    Rancher
    Posts: 1776
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic