Introduction to Object

Introduction to Inheritance
CSIS 3701: Advanced Object Oriented Programming
Reuse
• Reuse in maintenance
– Must be able to add abilities to new version without
causing faults in existing code
– Otherwise, will have to redesign/retest entire program
for each modification!
Existing code from
previous version
New code affects
existing code
New code to add
new abilities and
features
Reuse
• Reuse in version design
– Multiple products may share common features
– If those features changed, must propagate to each
product
Change to file
handling
Word
Common features
of Office products
(file handling, etc.)
Excel
PowerPoint
Inheritance Concept
• Superclass (base class) with core abilities
• Subclass (derived class) extends abilities
– Inherits member variables and methods from superclass
– Defines additional member variables and methods to
manipulate them
Subclass
Superclass
Superclass member variables
Superclass methods
Superclass member variables
Superclass methods
inherits
Additional member variables
Additional methods to
manipulate them
Inheritance Concept
Subclass
Superclass
Superclass member variables
Superclass methods
New variables and methods
Superclass member variables
Superclass methods
New variables and methods
inherits
Additional member variables
Additional methods to
manipulate them
Changes to superclass
propagated to subclass
Additional features in
subclass have no effect
on superclass
Clock Example
Clock
hour
minute
SecondClock
hour, minute
second
• SecondClock class extends Clock
– Additional member variable for second
– Additional methods to set second, get second,
validate second, increment second…
• Design methodology:
– Additional methods in subclass should only
manipulate additional member variables
Inheritance Syntax
• Subclass extends superclass
public class subclass extends superclass {
…
}
Subclass now has all member variables
and methods of superclass
public class SecondClock extends Clock {
• Every SecondClock object has:
– Hour and minute member variables
– nextMinute, toString, getHour, getMinute, setHour, setMinute, …
Inheritance Syntax
• Additional member variables and methods:
public class SecondClock extends Clock {
private int second;
public static final int MAXSECOND = 59;
public void setSecond(int s) {
if (isSecond(s)) second = s;
}
public int getSecond() {return second;}
public boolean isSecond(int s) {
return (s >= 0 && s < MAXSECOND);
}
Inheritance Syntax
• Subclass methods can call inherited superclass
methods
– Best way to manipulate inherited member variables
public void nextSecond() {
second++;
if (second > MAXSECOND) {
second = 0;
If second reaches 60, increment
nextMinute();
the current minute as well
}
}
Inheritance and Constructors
• Subclass constructor must invoke superclass
constructor
• Syntax:
super(parameters);
– Must be first statement in constructor
– May pass parameters (usually used to set initial
values of superclass member variables)
– If not invoked, automatically calls superclass no-op
constructor
Inheritance and Constructors
public SecondClock(int h, int m,
int s) {
super(h, m);
second = s;
}
s
• Example:
public Clock(int h,
int m) {
hour = h;
minute = m;
}
hour: 12
minute: 34
second: 56
SecondClock s = new SecondClock(12,34,56);
– Clock constructor passed 12, 34
– Clock constructor sets hour = 12 and minute = 56
– SecondClock constructor sets second = 56
Inheritance and Constructors
public Clock() {
hour = 0;
minute = 0;
}
public SecondClock() {
second = 0;
}
s
hour: 0
minute: 0
second: 0
• If no call to super, then no-parameter superclass
constructor called by default
SecondClock s = new SecondClock();
– Default Clock constructor sets hour = 0 and minute = 0
– SecondClock constructor sets second = 0
Overriding Superclass Methods
• May need to redefine inherited method to include
additional sub class member variables
• Can override inherited method by putting new version in
the subclass
• Example: inherited toString() method
– Should also display current second
Overriding Superclass Methods
public class SecondClock extends Clock {
…
New version of
public String toString() {
toString() in
SecondClock class
String result = "";
if (getHour() < 10) result += "0";
result += getHour() + ":";
if (getMinute() < 10) result += "0";
result += getMinute() + ":";
if (second < 10) result += "0";
result += second;
Adds information from
additional member
return result;
variable second
}
…
}
Calling Superclass Methods
• Better solution: just call superclass version of
toString() to show hour, minute
public String toString() {
String output = toString() + ":";
if (second < 10) output += "0";
output += second;
return output;
}
• Problem: infinite recursive loop!
Calling Superclass Methods
• Must indicate you are calling superclass version of method
• Syntax: super.methodname (parameters)
public String toString() {
String output = super.toString() + ":";
if (second < 10) output += "0";
output += second;
return output;
}
• Design methodology:
Overridden method should always call superclass version
to insure superclass variables used in consistent way
Overriding Superclass Methods
• Compiler determines which version to call based
on object type
Clock c = new Clock(6, 37);
SecondClock s = new SecondClock(10, 19, 41);
System.out.println(c.toString());
System.out.println(s.toString());
Calls original
superclass
version
Calls overridden
subclass version
Inheritance vs. Composition
• Key point:
– Subclass object is a type of superclass object (with
additional features)
– It does not contain an instance of a superclass
• Example:
Car
extends
GMCar
extends
Cruze