• 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

To which class, object is refering to?  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

below is my sample program



From line no 13, i can clearly make out that "sc" is of SubClass object which refers to SuperClass reference.
I was able to display that in line no 15, with sc.getClass(), which shows to which class the object belongs.

Is there any way i can display to which class "sc" (in this case) is referencing to?

 
Marshal
Posts: 6285
1067
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.lang.Class has a method getSuperclass(), which can be used to get the Class object which represents the super class of it. You can also use the getGenericSuperclass() which returns a Type object that representing the direct super class of the given object.

This is not available in SCJP objectives. For further discussion of this, or for any other reflection-based questions, please post in an appropriate forum.

Devaka
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But still you cannot find the exact class with which the reference was declared. This is because there can be multiple level of inheritance like in your example, you can declare sc to be of type Object...
 
Devaka Cooray
Marshal
Posts: 6285
1067
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ankit Garg wrote:But still you cannot find the exact class with which the reference was declared. This is because there can be multiple level of inheritance like in your example, you can declare sc to be of type Object...


I don't think there is a way to get the exact reference type at runtime.
 
Madhu Desai
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Devaka Cooray wrote:This is not available in SCJP objectives. For further discussion of this, or for any other reflection-based questions, please post in an appropriate forum.



Point noted..

But this was definitely related to SCJP (not of course about .getSuperclass())

i just wanted to know a better way to figure it out to which class object is referencing (line 14 and 17), like in the example:
K&B :Chapter 2 - Self test - Question 8 (Modified)



I wanted a way to confirm myself that what i thought is reference class is indeed true.

Thanks very much for your help
 
Devaka Cooray
Marshal
Posts: 6285
1067
Chrome Eclipse IDE Google App Engine IntelliJ IDE jQuery Postgres Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Madhu Desai wrote:I wanted a way to confirm myself that what i thought is reference class is indeed true.



That's really good
 
Ankit Garg
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Devaka Cooray wrote:

Ankit Garg wrote:But still you cannot find the exact class with which the reference was declared. This is because there can be multiple level of inheritance like in your example, you can declare sc to be of type Object...


I don't think there is a way to get the exact reference type at runtime.



That's what I was also trying to say
 
He's dead Jim. Grab his tricorder. I'll get his wallet and this tiny ad:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!