Question
Create another version of the Person class called PersonSafer. This class should not be subclassable, and should not allow any changes to the attribute data
Create another version of the Person class called PersonSafer. This class should not be subclassable, and should not allow any changes to the attribute data once they have been set, i.e. it should be immutable. Try the same process as before but using the PersonSafer class instead of Person/EvilPerson and ensure that it cannot be misused as the Person class can be.
Person Class:
public class Person { String name; String phone;
public Person() { name = new String(); phone = new String(); }
public Person(String name, String phone) { this.name = name; this.phone = phone; }
public void setName(String name) { this.name = name; }
public void setPhone(String phone) { this.phone = phone; }
public String getName() { return name; }
public String getPhone() { return phone; }
}
Evil Person Class:
public class EvilPerson extends Person { public EvilPerson(String name, String phone) { super(name,"n/a"); } public void setPhone(String phone) { this.phone = "n/a"; }
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started