Inheritance and Encapsulation CSIS 3701: Advanced Object Oriented Programming
Inheritance and Encapsulation Key question: What components of superclass do subclass methods have access to? Can subclass methods access private superclass member variables? Subclass Superclass Superclass variables Superclass methods Additional variables Additional methods Superclass variables Superclass methods inherits
Inheritance and Encapsulation Subclass methods have no access to private components of superclass public class SecondClock extends Clock { … public String toString() { String result = ""; if (hour < 10) result += "0"; result += hour + ":"; if (minute < 10) result += "0"; result += minute + ":"; if (second < 10) result += "0"; result += second; return result; } Compiler error hour and minute are private to the Clock class
Inheritance and Encapsulation Why does a subclass not have access to its own variables? Key idea: developers of superclass and subclass may not be the same Clock int hour int minute SecondClock Code that directly refers to hour and minute Fred develops Clock Does not know that Barney has extended it Barney extends Clock
Inheritance and Encapsulation If developer of superclass changes internal representation, subclass will no longer work! Clock String hour String minute SecondClock Code that directly refers to hour and minute as integers Fred changes Clock Does not know that it will affect Barney’s class SecondClock no longer works
Public Access as Solution Solutions: Only access superclass variables using superclass public methods public String toString() { String result = ""; if (getHour() < 10) result += "0"; result += getHour() + ":"; if (getMinute() < 10) result += "0"; result += getMinute() + ":"; … Problem: What if no such public methods exist in superclass?
NameList Example Example: FlexName class extends NameList Override add for list full case: Increment maximum Rebuild names array to be one element longer Problem: these are private to NameList No public methods to allow access, either public class FlexName extends NameList { … public void add(String name) { if (isFull()) { maximum++; …
Protected Encapsulation Declare component as protected encapsulation May be accessed by subclasses but not other classes public class NameList { protected int maximum; protected int count; protected String[] names; … public class FlexName extends NameList { … public void add(String name) { if (isFull()) { maximum++; … Since FlexName extends NameList, this is now legal
Protected Encapsulation Problem: Developer of superclass may want to change internal representation in future No longer possible without affecting derived classes Better approach: Provide protected methods Other classes still have no access If internal representation changes, can just change method so behavior is still the same
Protected Methods public class NameList { private int maximum; … protected int getMaximum() {return maximum;} protected void setMaximum(int m) {maximum = m;} Other classes cannot use these public class FlexName extends NameList { … public void add(String name) { if (isFull()) { setMaximum(getMaximum() + 1); …