Presentation is loading. Please wait.

Presentation is loading. Please wait.

Object-Oriented Programming 95-712 MISM/MSIT Carnegie Mellon University Professor Roehrig’s Notes.

Similar presentations


Presentation on theme: "Object-Oriented Programming 95-712 MISM/MSIT Carnegie Mellon University Professor Roehrig’s Notes."— Presentation transcript:

1 Object-Oriented Programming 95-712 MISM/MSIT Carnegie Mellon University Professor Roehrig’s Notes

2 A Digression: Arithmetic public class Node { public Node() {} public double eval() { System.out.println("Error: eval Node"); return 0; } public class Binop extends Node { protected Node lChild, rChild; public Binop(Node l, Node r) { lChild = l; rChild = r; }

3 Arithmetic (cont.) public class Plus extends Binop { public Plus(Node l, Node r) { super(l, r); } public double eval() { return lChild.eval() + rChild.eval(); } public class Const extends Node { private double value; public Const(double d) { value = d; } public double eval() { return value; } }

4 Arithmetic (cont.) public class TestArithmetic { // evaluate (1.1 + 2.2) + 3.3 public static void main(String[] args) { Node n = new Plus( new Plus( new Const(1.1), new Const(2.2)), new Const(3.3)); System.out.println(""+ n.eval()); } + + 2.21.1 3.3

5 Node n1 = new Const(1.1); Node n2 = new Const(2.2); Node n3 = new Plus(n1, n2); Node n4 = new Const(3.3); Node n5 = new Plus(n3, n4);

6 Arithmetic (cont.) Binary operators create a binary tree. Binary operators create a binary tree. Constants are “leaf” nodes. Constants are “leaf” nodes. We could easily add more operators and terminals. We could easily add more operators and terminals. + + 2.21.1 3.3

7 Extensibility The Arithmetic program allows additional subclasses to be constructed and easily integrated. The Arithmetic program allows additional subclasses to be constructed and easily integrated. Polymorphism is the key in letting this happen. Polymorphism is the key in letting this happen. We should never make Node objects. We should never make Node objects. How to prevent this? How to prevent this?

8 Abstract Classes As always, get the compiler involved in enforcing our decisions. As always, get the compiler involved in enforcing our decisions. Now it’s a compiler error if we try to make a Node object (but Node references are OK). Now it’s a compiler error if we try to make a Node object (but Node references are OK). Subclasses are abstract (and we must so state) until all abstract methods have been defined. Subclasses are abstract (and we must so state) until all abstract methods have been defined. public abstract class Node { public Node() {} public abstract double eval(); }

9 Order of Construction Put print statements into the constructors in the Arithmetic example. The output is: Put print statements into the constructors in the Arithmetic example. The output is: Node constructor Const constructor 1.1 Node constructor Const constructor 2.2 Node constructor Binop constructor Plus constructor Node constructor Const constructor 3.3 Node constructor Binop constructor Plus constructor 6.6 + + 2.21.1 3.3 Node n = new Plus( new Plus( new Const(1.1), new Const(2.2)), new Const(3.3));


Download ppt "Object-Oriented Programming 95-712 MISM/MSIT Carnegie Mellon University Professor Roehrig’s Notes."

Similar presentations


Ads by Google