2

I'm using a Processing library to build my project in Java. I use a function that returns me an object of type PShape (which I don't have access to source).

I need to make this object of type Shape (a class I designed that extends PShape).

How can I make that?

Basically I have:

PShape pShape = loadShape(filename);

Where loadShape is a function I don't have access to source code.

I want to somehow do:

class Shape extends PShape {...}

and then

Shape shape = (Shape) loadShape(filename);

But it won't work, once loadShape() will give me a PShape, not a Shape

How can I make loadShape returns a Shape?

Thank you

1 Answer 1

7

If loadShape() returns a PShape, then it returns a PShape. You can't make it return a subclass of PShape.

Easiest approach would be Shape either copies the PShape into a new instance: e.g.

Shape myLoadShape(String filename)
{
    return new Shape(loadShape(filename));
    // Assumes you have a `Shape(PShape)` constructor.
}

or perhaps Shape isn't a subclass, but it contains a PShape data member.

class Shape
{
    // No one picked up my C++ syntax goof ;-)
    protected PShape pshape;

    // Using a constructor is just one way to do it.
    // A factory pattern may work or even just empty constructor and a
    // load() method.
    public Shape(String filename)
    {
        pshape = loadShape(filename);
        // Add any Shape specific setup
    }


}
Sign up to request clarification or add additional context in comments.

10 Comments

To extend the answer: What differentiates the Shape class from the PShape class? Exactly add that into the above code for creating a Shape object using an existing PShape object.
I didn't want to use the Decorator Pattern or having the PShape object as a member of Shape. How can I have a Shape(PShape) constructor and PShape not being a member of Shape?
@Zabuza, I need to add some functionalities to PShape object and use it in other methods (which expects a PShape input and returns a PShape object)
Well, your desired structure is not totally clear to me. Imagine PShape being a class Animal and Shape a class Dog. It directly follows that Dog inherits all properties of an Animal but a method that acts on Animal will not be able to use the exclusive properties of a Dog as not all Animal can for example bark. However you can indeed pass a Dog into a method that wants Animal. But you will not be able to cast an Animal to a Dog, a Cat is no Dog.
@Zabuza. It's the opposite I am wanting to achieve. Imagine PShape being a class Dog. I want Shape to be a class Animal. So whenever I create a Dog using the desired method, I want to actually create an Animal, because the Dog class only have bark() method (and I can't edit it) and I want to add a bite() method. (All the Animals I'll have are going to be Dogs)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.