Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

To which class, object is refering to?

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 6914
1314
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 6914
1314
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 6914
1314
IntelliJ IDE jQuery Eclipse IDE Postgres Database Tomcat Server Chrome Google App Engine
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Paper jam tastes about as you would expect. Try some on this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic