Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

calling Object class methods using an interface reference variable

 
meenakshi ashokkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
meenakshi ashokkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i meant the PUZZLE wrt the way interfaces work
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
meenakshi ashokkumar
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks very much for the enlightening answer Joanne & thanks others too.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic