Reputation: 4194
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
Upvotes: 2
Views: 1482
Reputation: 29266
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
}
}
Upvotes: 7