Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

how to write a wrapper class for an Object rather than an Interface

807606May 24 2007 — edited May 25 2007
I have an Object I want to modify.
I'd like to put a wrapper class around it.
so I can keep the current object + just modify the data returned from it.

Say I have a Car Object and I want to create a Pimped wrapper class.
I can make a PimpedCar Class that extends Car and takes the original Car as a param in its constructor.
public class CarPimper extends Car{
   private Car carToPimp;
   public PimpedCar( Car car){carToPimp = car;} 
   public Image getImage( ){
      Image carImage = carToPimp.getImage();
      addSpeedStripes( carImage );
      return carImage;
   } 
   public int getNumWheels( ){return carToPimp.getNumWheels();}
   private void addSpeedStripes( Image image){
      //whatevers needed
   } 
}
To code this way I have to override every method in Car and eithor just return the data from the same method for carToPimp or adapt the data before I return it.
This is all very well except that some of the methods I want to override + call are final. So I can't override them.
I guess it's poor that my Car Object is not an interface, but thats somthing that I can't change.
So I'm stuck - is there any other way for me to do this?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 22 2007
Added on May 24 2007
29 comments
1,005 views