• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

everything is an object ?

 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

please look at following code



The code above compiles and runs successfully. The question is why the reference variable t1 allow a call to the equals() method? Every class in java extends Object by default, but an interface does not extend Object, because an interface can only extend another interface but not class. The interface anInterface does not define the method equals() but a call to this method is still possible. can anyone tell me how it works?

kind regards
Serg
 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Serg,

but don't forget that in java whenever you use the keyword "new", you're creating an Object. That's the reason. Be careful, you are creating a "anInterface" reference that is reffering to a "TestanInterface" object.

Hope this will help.
[ April 17, 2008: Message edited by: Mamadou Tour� ]
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


The question is why the reference variable t1 allow a call to the equals() method? Every class in java extends Object by default, but an interface does not extend Object, because an interface can only extend another interface but not class. The interface anInterface does not define the method equals() but a call to this method is still possible. can anyone tell me how it works?



well the reason you can access the equals() method is that t1 is pointing to an object of type TestanInterface, and TestanInterface derives from Object class thats why.


Hope this helps
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Serg,

You are doing upcasting in this case.

anInterface t1 = new TestanInterface();
anInterface t2 = new TestanInterface();

t1 and t2 are the two references to the objects of testanInterface class. Hence you can directly call equls method on t1.

Thanks
Veeresh
 
Amit Ghorpade
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just missed the above poster by 2 min


and i miss it again...
[ April 17, 2008: Message edited by: Amit Ghorpade ]
 
Serg Masow
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

thanks for your postings. That the class TestanInterface extends Object is obvious. But a not the created instance decides which of the methods can be called but the type of the reference variable. To explain it let me modify the code a litte:



So you can see, altought the anAnotherMethod() is defined in the class, you can not call it. It does not matter what for methods the instance have, without explicit cast only methods defined in the interface can be called. But somehow knows the interface the methods of the class Object... That is the question, from where knows the interface the methods of the class Object?

kind regards
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
References always refer to an Object, even if the type of the reference is an interface. This only expresses that the only thing you know about the referenced Object is that it implements a certain interface. To be able to be instantiated, it has to be a an actual class, though, even if you don't know which. But you (and the compiler) inherently know that this class is a sub class of java.lang.Object, basically because all java classes are. Therefore you can invoke equals() on any reference.
 
Serg Masow
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guido,

that's a good explanation. Thanks a lot.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the Java Language Specification specifically states that the members of an interface by definition include all the methods declared by Object. This specifically takes care of the problem that Serg Masow pointed out.
 
Serg Masow
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

many thanks, it's what I looked for. A reference to JLS is always good, even if sometimes it really hard to understand.
 
reply
    Bookmark Topic Watch Topic
  • New Topic