Upload
jorn
View
39
Download
0
Tags:
Embed Size (px)
DESCRIPTION
Applying Inheritance to Solve problems. Applied Inheritance. Decide on the classes involved from the problem description Group classes into conceptually equivalent groups Per group of classes, the generalizing concept will be a Parent to those classes - PowerPoint PPT Presentation
Citation preview
Designing with inheritance (c) Eraj Basnayake
1
Applying Inheritance to Solve problems
Designing with inheritance (c) Eraj Basnayake
2
Applied Inheritance
bullDecide on the classes involved from the problem description
bullGroup classes into conceptually equivalentconceptually equivalent groups
bullPer group of classes the generalizing concept will be a Parent to those classes
bullDistribute responsibilities amongst allall classes based on what is appropriateclasses based on what is appropriateand customary for that class
Example
A program to teach geometry needs to model some basic shapes All shapes
have surface area could have volume and location The shapes that
need to be modeled are Circle Rectangle Sphere Cylinder and a Square
What classes will you need Draw an UML diagram showing class relationships
if any
Designing with inheritance (c) Eraj Basnayake
3
ExampleClasses
Circle Rectangle Sphere Cylinder Square Coordinate
All ldquoShapesrdquo
Square is a Rectangle
Coordinate
3D Shapes
2D Shapes
Inheritance hierarchy
Group classes
Shape
2D Shapes 3D Shapes
Circle Rectangle
Square
Cylinder Sphere
Circle
Rectangle
SquareCylinder
Sphere
Designing with inheritance (c) Eraj Basnayake
4
Shape
Shapes2D Shapes3D
Circle Rectangle
Square
Cylinder Sphere
Examplehellip
Distribute Responsibilities
Shape
+getSurfaceArea()
+clone()
+equals()
+toString()
+Object getLocation()
Shape2D
+
Shape3D
+getVolume()
Is Shape2D needed
Designing with inheritance (c) Eraj Basnayake
5
public abstractabstract class Shape
public abstract double getSurfaceArea()
public abstract Object getLocation()
public abstract Object clone()
public abstract String toString()
public abstract boolean equals()
Example hellip Abstract classes
Observe the syntax of Shape
The methods are not empty methods
Abstract ClassAbstract Class A class with at least oneat least one unimplemented method
InterfaceInterface A class with onlyonly unimplemented methods
Shape above is an example of an Abstract class
public interfaceinterface Shape
public double getSurfaceArea()
public Object getLocation()
public Object clone()
public String toString()
public boolean equals()
Designing with inheritance (c) Eraj Basnayake
6
Abstract Classesbull An abstract class is a placeholder
ndash It is too generic to be of use by itself ndash It is used in a class hierarchy to organize common features at appropriate
levels bull Abstract class cannot be instantiated
ndash However an abstract class variable can reference any concrete derived object
bull An abstract method has no implementation just a signaturendash It is never intended to be called directly (a placeholder)ndash Abstract methods can appear only in classes that themselves been
declared abstract bull The modifier abstract is used to define abstract classes amp methods
ndash The children of the abstract class are expected to define implementations for the abstract methods in ways appropriate for them
bull 1048715If a child class does not define all abstract methods of the parent then the child is also abstract
Designing with inheritance (c) Eraj Basnayake
7
Interfaces vs Abstract classes how to decide
Not all programming languages have Interfacersquos
1 unim
plem
ente
d
met
hod2
unimple
men
ted
met
hodAll
met
hods
unimple
men
ted
All m
ethods
imple
men
ted
Regular
Class
Abstract
classInterface
hellip
So why have abstract classes and interfaces
bullTo enforce design conformitybullClients can expect a uniform set of methods for related classesbullInterfacersquos are used to ldquotagrdquo a class with a property While interfaces
are classesare classes they represent ldquoaction ideasrdquo ldquoverbsrdquo or ldquopropertiesrdquo
about class
Designing with inheritance (c) Eraj Basnayake
8
InterfacesSample Interfacersquos
Cloneable Drawable Colorable Runable Comparable hellip
Q How do you inherit from an interface
A By implementrsquoing the interface
public interface Shape implements Cloneable
hellip
Q How do you inherit from an Abstract class
A The usual way by extendrsquoing
public abstract class Shape2D implements Shape
hellip
public class Circle extendsextends Shape2D
hellip
extends
Designing with inheritance (c) Eraj Basnayake
9
Its all still inheritance
public interface Shape implements Clonable
hellip
public abstract class Shape2D implements Shape
hellip
public class Circle extends Shape2D
Circle has to implement all unimplemented methods ofbull 2DShapebull Shapebull Clonable
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D)s
extends
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
2
Applied Inheritance
bullDecide on the classes involved from the problem description
bullGroup classes into conceptually equivalentconceptually equivalent groups
bullPer group of classes the generalizing concept will be a Parent to those classes
bullDistribute responsibilities amongst allall classes based on what is appropriateclasses based on what is appropriateand customary for that class
Example
A program to teach geometry needs to model some basic shapes All shapes
have surface area could have volume and location The shapes that
need to be modeled are Circle Rectangle Sphere Cylinder and a Square
What classes will you need Draw an UML diagram showing class relationships
if any
Designing with inheritance (c) Eraj Basnayake
3
ExampleClasses
Circle Rectangle Sphere Cylinder Square Coordinate
All ldquoShapesrdquo
Square is a Rectangle
Coordinate
3D Shapes
2D Shapes
Inheritance hierarchy
Group classes
Shape
2D Shapes 3D Shapes
Circle Rectangle
Square
Cylinder Sphere
Circle
Rectangle
SquareCylinder
Sphere
Designing with inheritance (c) Eraj Basnayake
4
Shape
Shapes2D Shapes3D
Circle Rectangle
Square
Cylinder Sphere
Examplehellip
Distribute Responsibilities
Shape
+getSurfaceArea()
+clone()
+equals()
+toString()
+Object getLocation()
Shape2D
+
Shape3D
+getVolume()
Is Shape2D needed
Designing with inheritance (c) Eraj Basnayake
5
public abstractabstract class Shape
public abstract double getSurfaceArea()
public abstract Object getLocation()
public abstract Object clone()
public abstract String toString()
public abstract boolean equals()
Example hellip Abstract classes
Observe the syntax of Shape
The methods are not empty methods
Abstract ClassAbstract Class A class with at least oneat least one unimplemented method
InterfaceInterface A class with onlyonly unimplemented methods
Shape above is an example of an Abstract class
public interfaceinterface Shape
public double getSurfaceArea()
public Object getLocation()
public Object clone()
public String toString()
public boolean equals()
Designing with inheritance (c) Eraj Basnayake
6
Abstract Classesbull An abstract class is a placeholder
ndash It is too generic to be of use by itself ndash It is used in a class hierarchy to organize common features at appropriate
levels bull Abstract class cannot be instantiated
ndash However an abstract class variable can reference any concrete derived object
bull An abstract method has no implementation just a signaturendash It is never intended to be called directly (a placeholder)ndash Abstract methods can appear only in classes that themselves been
declared abstract bull The modifier abstract is used to define abstract classes amp methods
ndash The children of the abstract class are expected to define implementations for the abstract methods in ways appropriate for them
bull 1048715If a child class does not define all abstract methods of the parent then the child is also abstract
Designing with inheritance (c) Eraj Basnayake
7
Interfaces vs Abstract classes how to decide
Not all programming languages have Interfacersquos
1 unim
plem
ente
d
met
hod2
unimple
men
ted
met
hodAll
met
hods
unimple
men
ted
All m
ethods
imple
men
ted
Regular
Class
Abstract
classInterface
hellip
So why have abstract classes and interfaces
bullTo enforce design conformitybullClients can expect a uniform set of methods for related classesbullInterfacersquos are used to ldquotagrdquo a class with a property While interfaces
are classesare classes they represent ldquoaction ideasrdquo ldquoverbsrdquo or ldquopropertiesrdquo
about class
Designing with inheritance (c) Eraj Basnayake
8
InterfacesSample Interfacersquos
Cloneable Drawable Colorable Runable Comparable hellip
Q How do you inherit from an interface
A By implementrsquoing the interface
public interface Shape implements Cloneable
hellip
Q How do you inherit from an Abstract class
A The usual way by extendrsquoing
public abstract class Shape2D implements Shape
hellip
public class Circle extendsextends Shape2D
hellip
extends
Designing with inheritance (c) Eraj Basnayake
9
Its all still inheritance
public interface Shape implements Clonable
hellip
public abstract class Shape2D implements Shape
hellip
public class Circle extends Shape2D
Circle has to implement all unimplemented methods ofbull 2DShapebull Shapebull Clonable
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D)s
extends
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
3
ExampleClasses
Circle Rectangle Sphere Cylinder Square Coordinate
All ldquoShapesrdquo
Square is a Rectangle
Coordinate
3D Shapes
2D Shapes
Inheritance hierarchy
Group classes
Shape
2D Shapes 3D Shapes
Circle Rectangle
Square
Cylinder Sphere
Circle
Rectangle
SquareCylinder
Sphere
Designing with inheritance (c) Eraj Basnayake
4
Shape
Shapes2D Shapes3D
Circle Rectangle
Square
Cylinder Sphere
Examplehellip
Distribute Responsibilities
Shape
+getSurfaceArea()
+clone()
+equals()
+toString()
+Object getLocation()
Shape2D
+
Shape3D
+getVolume()
Is Shape2D needed
Designing with inheritance (c) Eraj Basnayake
5
public abstractabstract class Shape
public abstract double getSurfaceArea()
public abstract Object getLocation()
public abstract Object clone()
public abstract String toString()
public abstract boolean equals()
Example hellip Abstract classes
Observe the syntax of Shape
The methods are not empty methods
Abstract ClassAbstract Class A class with at least oneat least one unimplemented method
InterfaceInterface A class with onlyonly unimplemented methods
Shape above is an example of an Abstract class
public interfaceinterface Shape
public double getSurfaceArea()
public Object getLocation()
public Object clone()
public String toString()
public boolean equals()
Designing with inheritance (c) Eraj Basnayake
6
Abstract Classesbull An abstract class is a placeholder
ndash It is too generic to be of use by itself ndash It is used in a class hierarchy to organize common features at appropriate
levels bull Abstract class cannot be instantiated
ndash However an abstract class variable can reference any concrete derived object
bull An abstract method has no implementation just a signaturendash It is never intended to be called directly (a placeholder)ndash Abstract methods can appear only in classes that themselves been
declared abstract bull The modifier abstract is used to define abstract classes amp methods
ndash The children of the abstract class are expected to define implementations for the abstract methods in ways appropriate for them
bull 1048715If a child class does not define all abstract methods of the parent then the child is also abstract
Designing with inheritance (c) Eraj Basnayake
7
Interfaces vs Abstract classes how to decide
Not all programming languages have Interfacersquos
1 unim
plem
ente
d
met
hod2
unimple
men
ted
met
hodAll
met
hods
unimple
men
ted
All m
ethods
imple
men
ted
Regular
Class
Abstract
classInterface
hellip
So why have abstract classes and interfaces
bullTo enforce design conformitybullClients can expect a uniform set of methods for related classesbullInterfacersquos are used to ldquotagrdquo a class with a property While interfaces
are classesare classes they represent ldquoaction ideasrdquo ldquoverbsrdquo or ldquopropertiesrdquo
about class
Designing with inheritance (c) Eraj Basnayake
8
InterfacesSample Interfacersquos
Cloneable Drawable Colorable Runable Comparable hellip
Q How do you inherit from an interface
A By implementrsquoing the interface
public interface Shape implements Cloneable
hellip
Q How do you inherit from an Abstract class
A The usual way by extendrsquoing
public abstract class Shape2D implements Shape
hellip
public class Circle extendsextends Shape2D
hellip
extends
Designing with inheritance (c) Eraj Basnayake
9
Its all still inheritance
public interface Shape implements Clonable
hellip
public abstract class Shape2D implements Shape
hellip
public class Circle extends Shape2D
Circle has to implement all unimplemented methods ofbull 2DShapebull Shapebull Clonable
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D)s
extends
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
4
Shape
Shapes2D Shapes3D
Circle Rectangle
Square
Cylinder Sphere
Examplehellip
Distribute Responsibilities
Shape
+getSurfaceArea()
+clone()
+equals()
+toString()
+Object getLocation()
Shape2D
+
Shape3D
+getVolume()
Is Shape2D needed
Designing with inheritance (c) Eraj Basnayake
5
public abstractabstract class Shape
public abstract double getSurfaceArea()
public abstract Object getLocation()
public abstract Object clone()
public abstract String toString()
public abstract boolean equals()
Example hellip Abstract classes
Observe the syntax of Shape
The methods are not empty methods
Abstract ClassAbstract Class A class with at least oneat least one unimplemented method
InterfaceInterface A class with onlyonly unimplemented methods
Shape above is an example of an Abstract class
public interfaceinterface Shape
public double getSurfaceArea()
public Object getLocation()
public Object clone()
public String toString()
public boolean equals()
Designing with inheritance (c) Eraj Basnayake
6
Abstract Classesbull An abstract class is a placeholder
ndash It is too generic to be of use by itself ndash It is used in a class hierarchy to organize common features at appropriate
levels bull Abstract class cannot be instantiated
ndash However an abstract class variable can reference any concrete derived object
bull An abstract method has no implementation just a signaturendash It is never intended to be called directly (a placeholder)ndash Abstract methods can appear only in classes that themselves been
declared abstract bull The modifier abstract is used to define abstract classes amp methods
ndash The children of the abstract class are expected to define implementations for the abstract methods in ways appropriate for them
bull 1048715If a child class does not define all abstract methods of the parent then the child is also abstract
Designing with inheritance (c) Eraj Basnayake
7
Interfaces vs Abstract classes how to decide
Not all programming languages have Interfacersquos
1 unim
plem
ente
d
met
hod2
unimple
men
ted
met
hodAll
met
hods
unimple
men
ted
All m
ethods
imple
men
ted
Regular
Class
Abstract
classInterface
hellip
So why have abstract classes and interfaces
bullTo enforce design conformitybullClients can expect a uniform set of methods for related classesbullInterfacersquos are used to ldquotagrdquo a class with a property While interfaces
are classesare classes they represent ldquoaction ideasrdquo ldquoverbsrdquo or ldquopropertiesrdquo
about class
Designing with inheritance (c) Eraj Basnayake
8
InterfacesSample Interfacersquos
Cloneable Drawable Colorable Runable Comparable hellip
Q How do you inherit from an interface
A By implementrsquoing the interface
public interface Shape implements Cloneable
hellip
Q How do you inherit from an Abstract class
A The usual way by extendrsquoing
public abstract class Shape2D implements Shape
hellip
public class Circle extendsextends Shape2D
hellip
extends
Designing with inheritance (c) Eraj Basnayake
9
Its all still inheritance
public interface Shape implements Clonable
hellip
public abstract class Shape2D implements Shape
hellip
public class Circle extends Shape2D
Circle has to implement all unimplemented methods ofbull 2DShapebull Shapebull Clonable
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D)s
extends
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
5
public abstractabstract class Shape
public abstract double getSurfaceArea()
public abstract Object getLocation()
public abstract Object clone()
public abstract String toString()
public abstract boolean equals()
Example hellip Abstract classes
Observe the syntax of Shape
The methods are not empty methods
Abstract ClassAbstract Class A class with at least oneat least one unimplemented method
InterfaceInterface A class with onlyonly unimplemented methods
Shape above is an example of an Abstract class
public interfaceinterface Shape
public double getSurfaceArea()
public Object getLocation()
public Object clone()
public String toString()
public boolean equals()
Designing with inheritance (c) Eraj Basnayake
6
Abstract Classesbull An abstract class is a placeholder
ndash It is too generic to be of use by itself ndash It is used in a class hierarchy to organize common features at appropriate
levels bull Abstract class cannot be instantiated
ndash However an abstract class variable can reference any concrete derived object
bull An abstract method has no implementation just a signaturendash It is never intended to be called directly (a placeholder)ndash Abstract methods can appear only in classes that themselves been
declared abstract bull The modifier abstract is used to define abstract classes amp methods
ndash The children of the abstract class are expected to define implementations for the abstract methods in ways appropriate for them
bull 1048715If a child class does not define all abstract methods of the parent then the child is also abstract
Designing with inheritance (c) Eraj Basnayake
7
Interfaces vs Abstract classes how to decide
Not all programming languages have Interfacersquos
1 unim
plem
ente
d
met
hod2
unimple
men
ted
met
hodAll
met
hods
unimple
men
ted
All m
ethods
imple
men
ted
Regular
Class
Abstract
classInterface
hellip
So why have abstract classes and interfaces
bullTo enforce design conformitybullClients can expect a uniform set of methods for related classesbullInterfacersquos are used to ldquotagrdquo a class with a property While interfaces
are classesare classes they represent ldquoaction ideasrdquo ldquoverbsrdquo or ldquopropertiesrdquo
about class
Designing with inheritance (c) Eraj Basnayake
8
InterfacesSample Interfacersquos
Cloneable Drawable Colorable Runable Comparable hellip
Q How do you inherit from an interface
A By implementrsquoing the interface
public interface Shape implements Cloneable
hellip
Q How do you inherit from an Abstract class
A The usual way by extendrsquoing
public abstract class Shape2D implements Shape
hellip
public class Circle extendsextends Shape2D
hellip
extends
Designing with inheritance (c) Eraj Basnayake
9
Its all still inheritance
public interface Shape implements Clonable
hellip
public abstract class Shape2D implements Shape
hellip
public class Circle extends Shape2D
Circle has to implement all unimplemented methods ofbull 2DShapebull Shapebull Clonable
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D)s
extends
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
6
Abstract Classesbull An abstract class is a placeholder
ndash It is too generic to be of use by itself ndash It is used in a class hierarchy to organize common features at appropriate
levels bull Abstract class cannot be instantiated
ndash However an abstract class variable can reference any concrete derived object
bull An abstract method has no implementation just a signaturendash It is never intended to be called directly (a placeholder)ndash Abstract methods can appear only in classes that themselves been
declared abstract bull The modifier abstract is used to define abstract classes amp methods
ndash The children of the abstract class are expected to define implementations for the abstract methods in ways appropriate for them
bull 1048715If a child class does not define all abstract methods of the parent then the child is also abstract
Designing with inheritance (c) Eraj Basnayake
7
Interfaces vs Abstract classes how to decide
Not all programming languages have Interfacersquos
1 unim
plem
ente
d
met
hod2
unimple
men
ted
met
hodAll
met
hods
unimple
men
ted
All m
ethods
imple
men
ted
Regular
Class
Abstract
classInterface
hellip
So why have abstract classes and interfaces
bullTo enforce design conformitybullClients can expect a uniform set of methods for related classesbullInterfacersquos are used to ldquotagrdquo a class with a property While interfaces
are classesare classes they represent ldquoaction ideasrdquo ldquoverbsrdquo or ldquopropertiesrdquo
about class
Designing with inheritance (c) Eraj Basnayake
8
InterfacesSample Interfacersquos
Cloneable Drawable Colorable Runable Comparable hellip
Q How do you inherit from an interface
A By implementrsquoing the interface
public interface Shape implements Cloneable
hellip
Q How do you inherit from an Abstract class
A The usual way by extendrsquoing
public abstract class Shape2D implements Shape
hellip
public class Circle extendsextends Shape2D
hellip
extends
Designing with inheritance (c) Eraj Basnayake
9
Its all still inheritance
public interface Shape implements Clonable
hellip
public abstract class Shape2D implements Shape
hellip
public class Circle extends Shape2D
Circle has to implement all unimplemented methods ofbull 2DShapebull Shapebull Clonable
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D)s
extends
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
7
Interfaces vs Abstract classes how to decide
Not all programming languages have Interfacersquos
1 unim
plem
ente
d
met
hod2
unimple
men
ted
met
hodAll
met
hods
unimple
men
ted
All m
ethods
imple
men
ted
Regular
Class
Abstract
classInterface
hellip
So why have abstract classes and interfaces
bullTo enforce design conformitybullClients can expect a uniform set of methods for related classesbullInterfacersquos are used to ldquotagrdquo a class with a property While interfaces
are classesare classes they represent ldquoaction ideasrdquo ldquoverbsrdquo or ldquopropertiesrdquo
about class
Designing with inheritance (c) Eraj Basnayake
8
InterfacesSample Interfacersquos
Cloneable Drawable Colorable Runable Comparable hellip
Q How do you inherit from an interface
A By implementrsquoing the interface
public interface Shape implements Cloneable
hellip
Q How do you inherit from an Abstract class
A The usual way by extendrsquoing
public abstract class Shape2D implements Shape
hellip
public class Circle extendsextends Shape2D
hellip
extends
Designing with inheritance (c) Eraj Basnayake
9
Its all still inheritance
public interface Shape implements Clonable
hellip
public abstract class Shape2D implements Shape
hellip
public class Circle extends Shape2D
Circle has to implement all unimplemented methods ofbull 2DShapebull Shapebull Clonable
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D)s
extends
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
8
InterfacesSample Interfacersquos
Cloneable Drawable Colorable Runable Comparable hellip
Q How do you inherit from an interface
A By implementrsquoing the interface
public interface Shape implements Cloneable
hellip
Q How do you inherit from an Abstract class
A The usual way by extendrsquoing
public abstract class Shape2D implements Shape
hellip
public class Circle extendsextends Shape2D
hellip
extends
Designing with inheritance (c) Eraj Basnayake
9
Its all still inheritance
public interface Shape implements Clonable
hellip
public abstract class Shape2D implements Shape
hellip
public class Circle extends Shape2D
Circle has to implement all unimplemented methods ofbull 2DShapebull Shapebull Clonable
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D)s
extends
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
9
Its all still inheritance
public interface Shape implements Clonable
hellip
public abstract class Shape2D implements Shape
hellip
public class Circle extends Shape2D
Circle has to implement all unimplemented methods ofbull 2DShapebull Shapebull Clonable
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D)s
extends
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
10
Its all still inheritance
Client Code
hellip
Circle c = new Circle()
hellip
Shape s = c
Cloneable cln = s
Shape2D s2d = (Shape2D) s
through c you can access ALL methods
of Circle AND its parents
Through s you can access ONLY methods
of Shape and Cloneable implemented in Circle
Through cln you can access ONLY methods
of Cloneable implemented in Circle
Through s2d you can access ONLY methods
of Shape Cloneable and Shape2D implemented
in Circle
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
11
Multiple Inheritance
Some programming languages allow for multiple inheritance
In Java
Can have multiple interface implementation
Single inheritance
Types of possible inheritance
bullAn interface can inherit from multiple interfaces by implementing
bullA class (abstract or otherwise) can inherit from multiple interfaces by
implementing
bullA class (abstract or otherwise) can inherit from a single other (abstract or
normal) class
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
12
A harder ExampleA Stack Machine is a device that lets you write simple programs that
use a Stack Items (in our example numbers) are placed on and removed
from the stack using two commands ldquopushrdquo to place on and ldquopoprdquo to take off
Items are always placed on top of the stack and taken off the top of the stack
A stack is a last-in-first-out structure
Arithmetic operations can be performed by assuming that the two operands
needed for the operation will be the top two items on the stack and the answer
will be placed back on the stack
push 5
5
Stack
push 10
5
Stack
1
0
add
15
Stack
Any mathematical expression can be converted into a series of machine instructions
5 + (6 (5 - 4))
push 5
push 4
subtract
push 6
multiply
push 5
add
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
13
Examplehellip What are the classes
Clients viewpublic static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoprogramFileNamerdquo)
smrun()
StackMachineCoordinate
all operations
void load(String fileName)
void run()
ProgramLoaderIts purpose is to read the
program in
void load(String filename)
void String getInstruction(int i)
int numInstructions()
StackIts purpose is to
model the Stack
idea
void push(double value)
double pop()
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
14
Examplehellip gaining an understanding
public class StackMachine
ProgramLoader pl
Stack stack
public StackMachine()
pl = new ProgramLoader()
stack = new Stack()
public void load(String fileName)
plload(fileName)
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
Systemoutprintln(instruction)
examplestk
push 5
push 4
subtract
push 6
multiply
push 5
add
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
What happens here
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
15
Examplehellip implementing the instructions - one solution
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase(ldquoPUSHrdquo)
String parameter = stnextToken()
stackpush(DoubleparseDouble(parameter))
else if (commandtoUpperCase(ldquoPRINTrdquo)
double d = stackpop()
Systemoutprintln(d)
hellip
Messy long yuck hellip
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
16
Examplehellip another way
Think of instruction as ldquoclassesrdquo and group theminstructions
push
pop
addsubtract
divide
multiply printone parameter
instruction no parameter
instruction
push
pop
add
subtract
dividemultiply
instructions
arithmetic instructions
stack
instructions
utility
instructions
Organized by
number of parameters
Organized by function
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
17
Examplehellip another way Instruction
OneParameterInstruction OneParameterInstruction
push pop add multiply divide subtractprint
Instruction
StackInstruction ArithmeticInstruction UtilityInstruction
push pop add subtract divide multiply print
bull Each instruction will know how to ldquoexecuterdquo itselfbull Each instruction will know how to initialize itself
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
18
Examplehellip another way
public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class ArithmeticInstruction implements Instruction
public void load(StringTokenizer st)
public class Add extends ArithmeticInstruction
public void execute(Stack stack)
double operand1 = stackpop()
double operand2 = stackpop()
stackpush(operand1+operand2)
Quick Review
Whatrsquos going on
hellip
Add a = new Add()
Instruction i = a
iload(st)
iexecute(stack)
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
19
Examplehellip another way public interface Instruction
public void load(StringTokenizer)
public void execute(Stack)
public abstract class StackInstruction implements Instruction
only here for design completeness
public class Pop extends StackInstruction
public void load(StringTokenizer st)
public void execute(Stack stack)
stackpop()
public class Push extends StackInstruction
double value
public void load(StringTokenizer st)
String parameter = stnextToken()
value = DoubleparseDouble(parameter)
public void execute(Stack stack)
stackpush(value)
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
20
Examplehellip another way
Will this workpublic class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
common
code
Is it any better
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
pload(st)
pexecute(stack)
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
pload(st)
pexecute(stack)
hellip
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
21
Examplehellip another way Will this work
public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
if (commandtoUpperCase()equals(ldquoPUSHrdquo))
Push p = new Push()
else if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
Subtract p = new Subtract()
else if hellip
hellip
pload(st)
pexecute(stack)
for
Common code
factored outWill this work
Why or why not
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
22
Examplehellip another way Will this work
public class StackMachine hellip public void run() String instruction for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)StringTokenizer st = new StringTokenizer(instruction)String command = stnextToken()
Instruction p if (commandtoUpperCase()equals(ldquoPUSHrdquo))
p = new Push() upcastelse if (commandtoUpperCase()equals(ldquoSUBTRACTrdquo))
p = new Subtract() upcasthellip
pload(st) method overriding
pexecute(stack) method overriding for
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that
Designing with inheritance (c) Eraj Basnayake
23
Examplehellip another way public class StackMachine
hellip
public void run()
String instruction
for(int i=0 iltplnumInstructions() i++)
instruction = plget(i)
StringTokenizer st = new StringTokenizer(instruction)
String command = stnextToken()
try
Instruction instRef = (Instruction) (ClassforName(command))newInstance() instRefload(st)
instRefexecute(stack)
catch(Exception e)
Systemoutprintln(ldquoSyntax error - bad instructionrdquo)
Systemexit(0)
Client code
public static void main(String[] args)
StackMachine sm = new StackMachine()
smload(ldquoexamplestkrdquo)
smrun()
A really cool way
to do all that