Skip to main content

Replacing an object instance within itself

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
danielpjdunbar
Offline
Joined: 2011-02-24

Hi,

I have written a small class to modle a door. When thinking of the interface (behaviour) I thought it would be nice to be able replace my door.

I am unfortunately stuck with the implementation of my void Replace() method. If anyone can help me to complete that method, I would really appreciate it.

This is my code:

package tutorial.realworld;
public class Door {
//Fields
String colour="";
String type="";
String currentPosition="";
int temperature=0;
boolean sounding=true;

//methods
void Install(String aType, String aPosition, String aColour){
type=aType;
currentPosition=aPosition;
colour=aColour;
sounding=false;
}

void Open(){
currentPosition="Open";
}

void Close(){
currentPosition="Close";
}

void StartKnocking(){
sounding = true;
}

void StopKnocking(){
sounding = false;
}

boolean Knocking(){
return sounding;
}

void Paint(String aColour){
colour=aColour;
}

void Replace(Door aNewDoor){
//this = aNewDoor; //Doesn't compile
//Please place code here
}

Any help is appreciated.

Kind regards,
Daniel

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
danielpjdunbar
Offline
Joined: 2011-02-24

Possibly my method (Replace) was too complex for the Door object.
If I had a Frame object, then sure, a replace(aNewDoor) would make sense, as there would be a field in Frame to hold the Door instance.
If in Door, if I have a Replace method, I am swapping two instances, so it doesn't make sence to have a Replace method in Door. Instead, I will just put in an "Uninstall()" method in Door and thats that.
Anyway, I think my interface was trying to do too much.
Regards,
Daniel