instances
i have a class called car, and three instances superCar, normalCar, and normalCarTwo.
also three variables which each instance can use - int speed, gear; boolean engineRunning;
when a supercar value gets changed, say speed = 50; i want that to change the other instance speeds as well, therefore all 3 instances would have a speed of 50.
but when i change one of the normal car's values, then it would not have an affect on any other instance, it only changes it's own variable value.
is this possible? can anyone show me how to do it?
thanks
[581 byte] By [
mark_8206a] at [2007-11-26 18:49:50]

Making a Car class, then SuperCar and NormalCar subclasses. I think you can achieve what you want through that. You know how to use static methods/variables right?
i know if i hadstatic int speed, then that would make all the instances values change.but i only want the supercar to change other values.
I already gave you a solution.
So, you've got something like this?
class Car {
int speed;
void setSpeed(int speed) { this.speed = speed; }
int getSpeed() { return speed; }
}
...
Car nc1 = new Car();
Car nc2 = new Car();
Car superCar = new Car();
And you want it that if you call nc1 or nc2.setSpeed(), it just affects that car, but if you call superCar.setSpeed(), it affects all three?
This is very strange, and probably not a good design. Can you explain why you want to do this?
Also, what are the specific rules driving this. That is, could there be additional normal cars or super cars? What determines if a car is normal or super? etc.
there will be no other normal or super cars, i'm trying to do this just to see if it's possible or not and practise writing code
> there will be no other normal or super cars, i'm
> trying to do this just to see if it's possible or not
> and practise writing code
It's possible, but how you do it depends on the details of what you're trying to do.
In this case, if you don't want a super/subclass relationship, then the Car class could have an additional member variable, a boolean called isSuper or something. You set that to true when you create the super and false for the normals. The set methods then check if that flag is true, and if it is, they call set on all the other cars.
The way it finds all the other cars is that there's a static member variable that lists all the cars, each car's constructor registers that car by inserting it into that static list.
If there could be more than one super, you'd have to prevent an infinite loop of one super's set calling anoter's calling the first's, etc.
This whole approach is somewhat of a hack, but without real requirements--real business rues--driving it, it's impossible to come up with a "good" way to do it because we have no measure of what "good" is--nothing real we're trying to accomplish.
thanks, i'll go give it a try
> when a supercar value gets changed, say speed = 50; i
> want that to change the other instance speeds as
> well, therefore all 3 instances would have a speed of
> 50.
> but when i change one of the normal car's values,
> then it would not have an affect on any other
> instance, it only changes it's own variable value.
> is this possible? can anyone show me how to do it?
> thanks
One way to solve this would be by the use of the Observer Pattern. In this design pattern objects (called Subjects) attach themselves to an Observer object. The idea now is that the Subject objects don't communicate between each other but via the common Observer object.
In your case all cars (Subjects) attach themselves to a command central (Observer). When a normal car changes it just changes but when a supercar changes it notifies the command central which then dispaches this news to all cars which then updates themselves accordingly.