• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

calling Object class methods using an interface reference variable

 
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:
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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
 
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.

 
Sheriff
Posts: 21783
103
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
create, convert, edit or print DOC and DOCX in Java
https://products.aspose.com/words/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!