Question: (b) Given below is the class Car. Explain why this class is mutable. What changes must be made to the class to make it immutable?

(b) Given below is the class Car. Explain why this class is mutable. What changes must be made

to the class to make it immutable? Re-write it making the necessary changes.

public class Car{

private String make, model, regNum;

public Car(String mdl, String mk, String reg){

model = mdl; make = mk; regNum = reg;

}

public String getMake(){return make;}

public String getModel(){return model;}

public String getRegNum(){return regNum;}

public void setModel(String m){model = m;}

public void setMake(String m){make = m;}

public void setRegNum(String r){regNum = r;}

public String toString() {return make + + model + + regNum;}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!