How to get the runtime class?

Discussion in 'Java' started by Fredy, Apr 29, 2005.

  1. Fredy

    Fredy Guest

    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?
     
    Fredy, Apr 29, 2005
    #1
    1. Advertising

  2. 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?


    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
     
    Patricia Shanahan, Apr 29, 2005
    #2
    1. Advertising

  3. Fredy

    Robert Guest

    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.
     
    Robert, Apr 29, 2005
    #3
  4. "Fredy" <> wrote in message news:d4u0t9$jcu$...
    > 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 http://www.iviz.com/
     
    Matt Humphrey, Apr 29, 2005
    #4
  5. 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?
     
    Abhijat Vatsyayan, Apr 29, 2005
    #5
  6. Robert wrote:
    > 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
     
    Patricia Shanahan, Apr 30, 2005
    #6
  7. Fredy

    Fredy Guest

    Yeah, sorry the bug was elsewere :)

    Abhijat Vatsyayan wrote:

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

    >
    >
     
    Fredy, Apr 30, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. .:[NeMo]:.

    Get interface of a class (runtime)

    .:[NeMo]:., Dec 26, 2003, in forum: Java
    Replies:
    1
    Views:
    547
    VisionSet
    Dec 28, 2003
  2. James D Carroll
    Replies:
    8
    Views:
    488
    Hemal Pandya
    Jun 27, 2004
  3. E11
    Replies:
    1
    Views:
    4,941
    Thomas Weidenfeller
    Oct 12, 2005
  4. Hal Vaughan
    Replies:
    11
    Views:
    1,195
    Gordon Beaton
    May 22, 2006
  5. Francis Hwang
    Replies:
    2
    Views:
    100
    Francis Hwang
    Jun 4, 2004
Loading...

Share This Page