Selective Inheritance?
In Java, it is not possible to select a subset of fields/methods to be inherited.
Moreover, it is not possible to change the visibility of inherited fields/methods from public
to private
.
There is the possibility to override the unwanted operations and change their behavior to e.g. display an error message or throw an exception, to indicate that the operation is not supported.
For example, following from the previous exercise, we can define Student
to extend GradStudent
yet not support the operations related to advisor
attribute, as follows.
public class Student extends GradStudent {
public Student(String name, String email) {
super(name, email);
}
@Override
public void setAdvisor(String advisor) {
throw new UnsupportedOperationException();
}
@Override
public String getAdvisor() {
throw new UnsupportedOperationException();
}
}
This strategy is not considered a good practice and must be avoided when possible.