aspose file tools*
The moose likes Java in General and the fly likes calling Object class methods using an interface reference variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "calling Object class methods using an interface reference variable" Watch "calling Object class methods using an interface reference variable" New topic
Author

calling Object class methods using an interface reference variable

meenakshi ashokkumar
Greenhorn

Joined: Mar 07, 2004
Posts: 22
Hi,

I have always thought and read that Using a superclass reference variable that is assigned a subclass object one can access only those members that are already defined in the superclass.

But in case of Interfaces, when I assign an interface reference variable an implementing class object I can call methods defined in the Object class like toString(), get Class(), equals() etc. Does this mean that Object is superclass of an interface also (I know thats a funny question to ask coz interfaces cant extend a class), but what explanation do we have for this possibility via interfaces?

interface A{}

class B implements A {
public static void main(String[] args){
System.out.println("Hello World!");
A a= new B();
System.out.println(a.getClass());//how is this possible?
System.out.println(a.toString());//how is this possible?
}
}










:roll: :roll: :roll:
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

No, class Object is not a superclass of interfaces. Interfaces do not have superclasses (because they are not classes...).

However, a reference variable always refers to an object (if it isn't null). Even if the type of the reference variable is an interface type, you can be sure that it points to an object that's an instance of a concrete (non-abstract) class that implements the interface. So you can be sure that the methods of class Object are available, so Java allows you to call those methods.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
meenakshi ashokkumar
Greenhorn

Joined: Mar 07, 2004
Posts: 22
thanks for taking the time and the explanation............

but still, the toString() method is in the implementing class, not the interface. SO making a statement like: A superclass reference variable can only access members that are efined in the superclass would not hold good for interfaces. For classes its always the overridden version of toString() that will be called using the superclass reference variable. But this is still a puzzle to me as to how it works.
meenakshi ashokkumar
Greenhorn

Joined: Mar 07, 2004
Posts: 22
i meant the PUZZLE wrt the way interfaces work
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3539
    
  15
Time to read the Java Language Specification

Specifically section 9.2 (slightly edited as I wasn't allowed to post a message with 'r' in it, unless I changed it to 'are'

If an interface has no direct superinterfaces, then the interface implicitly declares a public abstract member method m with signature s, return type rt, and throws clause t corresponding to each public instance method m with signature s, return type rt, 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.


Joanne
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Every object in Java is an Object in the end. If you take an object's class and go up the class hierarchy, you will ALWAYS end up at Object. The compiler knows this, and therefore it knows that the methods defined in Object are available.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Rob Prime:
Every object in Java is an Object in the end. If you take an object's class and go up the class hierarchy, you will ALWAYS end up at Object. The compiler knows this, and therefore it knows that the methods defined in Object are available.


Well, yeah, but if it were really that simple, the JLS wouldn't have the specific language above to deal with this situation. Joanne's excellent answer is the correct one.


[Jess in Action][AskingGoodQuestions]
meenakshi ashokkumar
Greenhorn

Joined: Mar 07, 2004
Posts: 22
thanks very much for the enlightening answer Joanne & thanks others too.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: calling Object class methods using an interface reference variable