Last active
December 31, 2015 11:49
-
-
Save ets/7981982 to your computer and use it in GitHub Desktop.
Class Instantiation Subtlety
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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