buschtoens
buschtoens

Reputation: 8541

Extend a returned Instance

I'm a Java newbie and ran into a bit of a problem. I want a class to become another class. It's hard to explain it the abstract way, so I'll give you an example.

public class WorldGuard extends WorldGuardPlugin {
    public WorldGuard(Plugin parent) {
        WorldGuardPlugin plugin = parent.getServer().getPluginManager().getPlugin("WorldGuard")
        // make plugin the actual class
    }
}

WorldGuard should act like some kind of a wrapper here. When constructed it gets one parameter parent on which base it finds an instance of WorldGuardPlugin. The class should now become that instance. It's simple in JavaScript, I just return the instance, but I can't do this in Java.

Upvotes: 0

Views: 91

Answers (4)

Sidharth Mudgal
Sidharth Mudgal

Reputation: 4264

You should call the copy constructor of WorldGuardPlugin. What I mean is that WorldGuardPlugin should have a constructor that can create a copy of a given instance of the class like:

WorldGuardPlugin pg = new WorldGuardPlugin(anInstance);

If this is the case then you are in luck. You can simply do:

public class WorldGuard extends WorldGuardPlugin {
    public WorldGuard(Plugin parent) {
        super( parent.getServer().getPluginManager().getPlugin("WorldGuard"));
    }
}

This will make "WorldGuard act like some kind of a wrapper here". You can still call the methods defined in WorldGuardPlugin on an instance of WorldGuard while being able to add methods to WorldGuard itself.

Upvotes: 1

AlexR
AlexR

Reputation: 115328

Class cannot become other class.

I think you can choose among the following possibilities.

  1. User Factory that creates instance of your classes. The factory will choose concrete class according to any logic you want and create instance of "right" class. If you want all classes the factory operates with can implement specific interface, so caller will not even know instance of which class is created.
  2. Use wrapper pattern. In this case your actual WordGuard class will wrap actual instance of other class and delegate all calls there.
  3. Use dynamic proxy or byte code engineering solution. But it is much more complicated and is not the best solution in most cases.

Upvotes: 1

Jeff Storey
Jeff Storey

Reputation: 57192

You can't change the type of object being constructed in a constructor. A constructor, by definition, constructs that class (e.g. A's constructor creates a type A).

In your example, WorldGuard extends WorldGuardPlugin, which means that WorldGuard is a type of WorldGuardPlugin. Maybe there is a way to initialize the WorldGuardPluin class (using a call to super in the constructor) with the properties that you want.

Upvotes: 0

Jay
Jay

Reputation: 27474

I'm not quite sure what you're trying to do. A class cannot "become another class". But perhaps your problem is just that you're trying to use a constructor when you should be using a plain function. Maybe what you want to do is this:

public class WorldGuard extends WorldGuardPlugin
{
  public static WorldGuard getFromPlugin(Plugin parent)
  {
    return (WorldGuard) parent.getServer().getPluginManager().getPlugin("WorldGuard");
  }
}

That would get the object via parent and return it as a WorldGuard object.

Upvotes: 2

Related Questions