I just finished studying immutable objects and their advantages so I thought I'd create one of my own. Here is my employee class that extends a person class. It is not mutable since iI have getters to my mutable date objects.
package objects.objects;
import java.util.Date;
public class Person {
private final String name;
private final Date dateOfBirth;
Person(String name,Date dateOfBirth){
this.name=name;
this.dateOfBirth=dateOfBirth;
}
public String getName() {
return name;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
}
EmployeeEmployee
Class:
Don't share references to the mutable objects. Never store references to external, mutable objects passed to the constructor; if necessary, create copies, and store references to the copies. Similarly, create copies of your internal mutable objects when necessary to avoid returning the originals in your methods
Furthermore, the Oracle doc says:
Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods.
What if I want my class to be subclassed? What harm is there if I don't make my class final or have a private constructor?
Also, could you point out apart from the date being mutable object references, is? Is there any other flaw in my code that violates the mutable policy or any other improvements as such?
Edit: furthermore The oracle doc says : "Don't allow subclasses to override methods. The simplest way to do this is to declare the class as final. A more sophisticated approach is to make the constructor private and construct instances in factory methods"... What if i want my class to be subclassed? what harm is there if i dont make my class final or have a private constructor? I am unable to follow this line??