Re: How to get the runtime class?
- From: Abhijat Vatsyayan <abhijat.vatsyayan@xxxxxxx>
- Date: Fri, 29 Apr 2005 15:52:49 -0400
Have you even tried compiling and running the example you have posted?
A few things -
* type of "c" will always be java.lang.Class .
* c.getName() can never return foo (or its fully qualified name). foo is an abstract
class.
* "this" will always refer to an object . If the object is of type foo2,
this.getClass().getName() will return foo2 (FQN really ) . There is no "this" at a class
level in java since class of an object is not really an object (in any interesting/OO
way) in java. Class of an object will always be an object of type java.lang.Class.
* Children object and parent object ? Are you talking inheritence or some kind of
composition ? If you wan to find out all subclasses of a given class, the simple answer
is - you can not. You can however find out super class or super interface(s) using
reflection.
Fredy wrote:
> I have 2 classes
>
> abstract public class foo {
>
> public void bar() {
>
> // i want a Class object of "this"
>
> Class c = this.getClass(); // here i get a class of type foo, even if
> the runtime class is type foo2
> }
> }
>
> public class foo2 extends foo {
>
> ...
> }
>
> with other words, how to get the children object from the parent object?
.
- Follow-Ups:
- Re: How to get the runtime class?
- From: Fredy
- Re: How to get the runtime class?
- References:
- How to get the runtime class?
- From: Fredy
- How to get the runtime class?
- Prev by Date: Re: How to get the runtime class?
- Next by Date: Re: Static initilizer failed to execute?
- Previous by thread: Re: How to get the runtime class?
- Next by thread: Re: How to get the runtime class?
- Index(es):
Relevant Pages
|