Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: clarified NCCE language

...

This noncompliant example invokes the doLogic method from the constructor. The super class doLogic method gets invoked on the first call, however, the overriding method is invoked on the second one. In the second call, the issue is that the constructor for SubClass initiates the super class's constructor which undesirably ends up calling SubClass's doLogic() method. The value of color is left as null because the initialization of the BaseClass class SubClass has not yet concluded.

Code Block
bgColor#FFcccc
class BaseClass {
  public BaseClass() {
    doLogic();
  }
	
  public void doLogic() {
    System.out.println("This is super-class!");
  }	
}

class SubClass extends BaseClass {
  private String color = null;
  public SubClass() {
    super();	
    color = "Red";
  }
	
  public void doLogic() {
    System.out.println("This is sub-class! The color is :" + color); //color becomes null
    //other operations
  }
}

public class Overridable {
  public static void main(String[] args) {
    BaseClass bc = new BaseClass(); //prints "This is super-class!"
    BaseClass sc = new SubClass();  //prints "This is sub-class! The color is :null"		
  }
}

...