How to get the runtime class?

F

Fredy

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?
 
P

Patricia Shanahan

Fredy said:
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?

I think you may have a problem in your testing.

public abstract class ClassTest {
public static void main(String[] args) {
ClassTest t = new foo2();
System.out.println(t.getClassName());
}
String getClassName(){
return getClass().getName();
}
}

class foo2 extends ClassTest{
}


prints "foo2".

Patricia
 
R

Robert

I think it'll work in any method that the child would call inside the
parent. Just probably not the ctor. Try any other method define the
method in the abstract class and have the derived class call it but
don't define it in the derived class. I bet it works.
 
M

Matt Humphrey

Fredy said:
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

this.getClass() returns the actual class of the object, not simply the
class of the current method. If you're getting "foo" instead of "foo2" for
an object that is of class "foo2" you have some other problem. Can you show
a short, complete example of code where this happens?
}
}

public class foo2 extends foo {

...
}

with other words, how to get the children object from the parent object?
getChildren () ? Your question doesn't make any sense in the context of
class types. Do you mean how can you get all the instances of a particular
class or all the subclasses of a particular class? You'll need to explain
what you want a little more.

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 
A

Abhijat Vatsyayan

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.
 
P

Patricia Shanahan

Robert said:
I think it'll work in any method that the child would call inside the
parent. Just probably not the ctor. Try any other method define the
method in the abstract class and have the derived class call it but
don't define it in the derived class. I bet it works.

Why would the constructor be any different?

public abstract class ClassTest {
String myClassName1 = getClass().getName();
String myClassName2;
ClassTest(){
myClassName2 = getClass().getName();
}
public static void main(String[] args) {
ClassTest t = new foo2();
System.out.println(t.getClassName());
System.out.println(t.myClassName1);
System.out.println(t.myClassName2);
}
String getClassName(){
return getClass().getName();
}
}

class foo2 extends ClassTest{
}


prints:

foo2
foo2
foo2
 
F

Fredy

Yeah, sorry the bug was elsewere :)

Abhijat said:
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:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top