Re: How to get the runtime class?



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?

.



Relevant Pages

  • Re: Wrapper class
    ... situations you should probably have a look at the proxy/decorator patterns ... public class FooProxy: Foo{ ... public override void Bar() { ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Newbie question - Disadvantages of java
    ... >> as I am about to switch to java, can somebody tell me the biggest ... public class Foo { ... public Foo getFoo() { ... I.e. primitive types, references and array references? ...
    (comp.lang.java.programmer)
  • Re: a case for multiple inheritance
    ... FYI The 'new' modifier will define a method as hiding a derived one, ... private new void Foo ... If you want to expose two read-only lists from your ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: XmlSerializer: Inheritance and Read-Only Properties
    ... > public class Bar: Foo ... > public string Message ...
    (microsoft.public.dotnet.xml)
  • Re: static virtual/override
    ... > I sorely miss the feature that permits ... > public class Bar: Foo ...
    (microsoft.public.dotnet.languages.csharp)