views:

83

answers:

7

Is there a way in Java to get an instance of my object's parent class from that object?

ex.

public class Foo extends Bar {

     public Bar getBar(){
          // code to return an instance of Bar whose members have the same state as Foo's
     }

}
A: 
return this;

If you want to return a different instance, then you can:

  • use a copy-constructor that will copy each field:

    return new Bar(this);
    
  • use some reflection utility, like BeanUtils:

    Bar bar = new Bar();
    BeanUtils.copyProperties(bar, this);
    return bar;
    
Bozho
+3  A: 

There's no built-in way to do this. You can certainly write a method that will take a Foo and create a Bar that's been initialized with the relevant properties.

  public Bar getBar() {
       Bar bar = new Bar();
       bar.setPropOne(this.getPropOne());
       bar.setPropTwo(this.getPropTwo());
       return bar;
  }

On the other hand, what inheritance means is that a Foo is a Bar, so you could just do

 public Bar getBar() {
      return this;
   }
JacobM
lemme give it a try
stevebot
Yes, all you have to do is write the method how you want it to work. If you just wanted it to `return this;`, then you should just do `Bar bar = foo;` You don't need a method to do that.
Erick Robertson
It's not unreasonable to have a method that "casts" a Foo to a Bar as part of an API, say if you want certain clients to use your Foo only as if it were a Bar.
JacobM
A: 

Long story short:

return this;

If you want to return a copy, then create a copy constructor on Bar that receives another Bar.

public Bar(Bar that) {
    this.a = that.a;
    this.b = that.b;
    ...
}
brunodecarvalho
A: 

this this is an instance of bar, the simple thing is just "return this;" but if you need a distinct object, perhaps you could implement java.lang.Clonable and "return this.clone();"

stew
A: 

If your class extends Bar, it is an instance of Bar itself. So

public Bar getBar() {
  return (Bar) this;
}

should do it. If you want a "different instance", you can try:

public Bar getBar() {
   return (Bar) this.clone();
}
krico
A: 

Since Foo is-a Bar, you can do this:

return this;

This will only return the parent instance of current object.

The Elite Gentleman
A: 

You can use reflection

    package com;

    class Bar {

        public void whoAreYou(){
            System.out.println("I'm Bar");
        }

    }

    public class Foo extends Bar{

        public void whoAreYou() {
            System.out.println("I'm Foo");
        }

        public Bar getBar() {
            try {
                return (Bar)this.getClass().getSuperclass().newInstance();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            throw new RuntimeException();
        }

        public static void main() {
            Foo foo = new Foo();
            foo.whoAreYou();
            foo.getBar().whoAreYou();
        }
    }
asela38