• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

How to get a base class subobject

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider class B extends class A.

For any code invoked from B, use of "this" returns an object of type B, such that this.getClass() gives "B".

How can one, if at all possible, get the base class subobject, that is, .getClass() should return A.
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
is this what you mean?

 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
More like this...


[ November 16, 2005: Message edited by: Stuart Ash ]
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi stuart,

since i know that it's you who normaly posts the answers, not the questions, i was warned ;-)

i discussed it with some colleagues. we'd say that it's not possible. my naive approach was to check
- if "this" holds a reference to B
- what is "super" holding then?

well, "this" is an object reference (can for example be passed into methods), while "super" is a way to reference a field in a superclass, and cannot be passed as an argument.

long talk, short sense:

it's not possible in my opinion.

curious about other answers,
jan
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd say you have to create an instance of A

 
Stuart Ash
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jan Groth:
hi stuart,

since i know that it's you who normaly posts the answers, not the questions, i was warned ;-)



Thanks Jan, sounds like a compliment.


Originally posted by Jan Groth:


i discussed it with some colleagues. we'd say that it's not possible. my naive approach was to check
- if "this" holds a reference to B
- what is "super" holding then?

well, "this" is an object reference (can for example be passed into methods), while "super" is a way to reference a field in a superclass, and cannot be passed as an argument.

long talk, short sense:

it's not possible in my opinion.

curious about other answers,
jan



I tried all ways too, and came to the same conclusion. And then I thought, maybe I have missed something somewhere, so I posted it here.


And, Adne, in response to your suggestion of getSuperClass(), what if A is abstract??
[ November 16, 2005: Message edited by: Stuart Ash ]
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Ash:

And, Adne, in response to your suggestion of getSuperClass(), what if A is abstract??



Then it cannot done.

getClass() returns "the runtime class of an object", and in order to have "A" returned from the getClass()-method, you need an object which is an instance of A without being an instance of any of A's subclasses.

If A is abstract, it cannot be instansiated as A. If you instanciate A as B, getClass() would return the more specific of the two - which is B.

 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now, recalling that, by definition, "new B() instanceof A" is true, can you tell us why you'd ever want to do this?
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suspect this was more like an academic exercise, "Is this possible or not?" If it had been possible, it can always be used for something
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stuart Ash:
Consider class B extends class A.

For any code invoked from B, use of "this" returns an object of type B, such that this.getClass() gives "B".

How can one, if at all possible, get the base class subobject, that is, .getClass() should return A.



I too am curious why you want to do this. For concrete superclass, you might be able to use reflection. The source code of Object{Input,Output}Stream might provide an insight.
 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm....
Getting more coffee...
[ November 18, 2005: Message edited by: Arun Kumarr ]
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this what you're trying to do?
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic