Skip to content

Instantly share code, notes, and snippets.

@ets
Last active December 31, 2015 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ets/7981982 to your computer and use it in GitHub Desktop.
Save ets/7981982 to your computer and use it in GitHub Desktop.
Class Instantiation Subtlety
class Base{
public String baseDeclared = null;
public Base(){
setDefaults();
}
public void setDefaults(){
baseDeclared = "DEFAULT BASED DECLARED VALUE";
}
}
class Child extends Base {
public String childDeclared = null;
public Child(){
super();
}
@Override
public void setDefaults(){
super.setDefaults();
childDeclared = "DEFAULT CHILD DECLARED VALUE";
baseDeclared = "BASED DECLARED VALUE CHANGED IN CHILD";
}
}
Child c = new Child();
//This assertion is true
assertEquals("baseDeclared not updated by child","BASED DECLARED VALUE CHANGED IN CHILD",c.baseDeclared);
//This assertion is false
assertEquals("childDeclared not set to default value","DEFAULT CHILD DECLARED VALUE",c.childDeclared);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment