Rule 5: Design and document for inheritance or else prohibit it
Refrain from Invoking overridable methods .because it may lead to fragility of method in case initiation of field is supposed to be done by sub class
public class SuperClass {
/**
* Broken Constructor invokes an overridable method
*/
public SuperClass() {
overrideMe();
}
protected void overrideMe(){
System.out.println("Super");
}
}
public class SubClass extends SuperClass {
private final Instant instant;
public SubClass() {
instant=Instant.now();
}
@Override
protected void overrideMe(){
System.out.print(instant);
}
public static void main(String[] args) {
SubClass sub=new SubClass();
sub.overrideMe();
//overrideMe();
}}
Use case:
Clonnable and Serializable Interfaces should be implemented with caution because they contains methods like clone() and readObject() which behaves as construcotr
so,refrain from calling overridable methods from [can a overridable methdo be classed feom clone and readObject from super class cause this issue] or else leads to fragility of code.
Summary-
futile to inherit immutable class and obvoius to inherit abstract class , normal classes should be refrained from inheritance,unless u document it properly to avoid calling the overridable method from the construcotr(class)