Family Polymorphism & Nested Inheritance Yevgen Voronenko.

Presentation on theme: "Family Polymorphism & Nested Inheritance Yevgen Voronenko."— Presentation transcript:

Family Polymorphism & Nested Inheritance Yevgen Voronenko

Problem: Java 1.4- class Node { bool touches(Edge e) {...e...} } class Edge { Node n1, n2; } class OnOffNode extends Node bool touches(Edge e) {...(OnOffEdge)e... } } class OnOffEdge extends Edge { bool enabled; }... new Edge(new Node, new Node)  OK new OnOffEdge(new OnOffNode, new OnOffNode)  OK new OnOffEdge(new OnOffNode, new Node)  Runtime Error

Node OnOffNode Edge OnOffEdge What We Have

Node OnOffNode Edge OnOffEdge What We Want Families

Solution 2: gbeta class Graph { class Node { bool touches(Edge e) {...e...} } class Edge { Node n1, n2; } } class OnOffGraph extends Graph { class Node { bool touches(Edge e) { if(enabled)...e... } } class Edge { bool enabled; } } new Graph.Edge(new Graph.Node, new Graph.Node)  Compile Error final g1=new Graph; new g1.Edge(new g1.Node, new g1.Node);  OK final g2=new OnOffGraph; new g2.Edge(new g2.Node, new g2.Node);  OK new g2.Edge(new g1.Node, new g2.Node);  Compile Error

Node Edge Graph OnOffGraph Not Families NodeEdge g1 : Graph Compile Time Runtime Family Solution 2: gbeta

Solution 3: Jx class Graph { class Node { bool touches(Edge e) {...e...} } class Edge { Node n1, n2; } } class OnOffGraph extends Graph { class Node { bool touches(Edge e) { if(enabled)...e... } } class Edge { bool enabled; } } new Graph.Edge(new Graph.Node, new Graph.Node)  Compile Error final Graph g1 = new Graph; new g1.class.Edge(new g1.class.Node, new g1.classNode);  OK final Graph g2 = new OnOffGraph; new g2.class.Edge(new g2.class.Node, new g2.class.Node);  OK new g2.class.Edge(new g1.class.Node, new g2.class.Node);  Compile Error new g1.class.Edge(new g1.class.Node, new g2.class.Node);  ??

Node Edge Graph OnOffGraph Families Compile Time Solution 3: Jx hard to understand new g1.class.Edge(new g1.class.Node, new g2.class.Node);  ?? hard to write final Graph g = new OnOffGraph(); final g.class.Node n = new g.class.Node; final g.class.Edge e = new g.class.Edge(n, n) e.touches(n);

Jx “problem” B B B2 A A2

Solution 4: Java 1.5+ class FNode[N extends FNode[N,E], E extends FEdge[N, E]] { bool touches(E e); } class FEdge[N extends FNode[N,E], E extends FEdge[N, E]] { N n1, n2; } class Node extends FNode[Node, Edge] { bool touches(Edge e) {...e... }} class Edge extends FEdge[Node, Edge] { } class OnOffNode extends FNode[OnOffNode, OnOffEdge] { bool touches(OnOffEdge e) {...e... }} class OnOffEdge extends FEdge[OnOffNode, OnOffEdge] { bool enabled; } new Edge(new Node, new Node)  OK new OnOffEdge(new OnOffNode, new OnOffNode)  OK new OnOffEdge(new Node, new OnOffNode)  Compile Error

Construction gbeta final Graph g = new OnOffGraph new g.Edge(new g.Node, new g.Node) Jx final Graph g = new OnOffGraph new g.class.Edge(new g.class.Node, new g.class.Node) Java 1.5 new OnOffEdge(new OnOffNode, new OnOffNode)

Construction gbeta final Graph g = new OnOffGraph new g.Edge(new g.Node, new g.Node) Jx final Graph g = new OnOffGraph new g.class.Edge(new g.class.Node, new g.class.Node) Java 1.5 new OnOffEdge(new OnOffNode, new OnOffNode)

Invoking touches() gbeta final Graph g = new OnOffGraph final g.Node n = new g.Node final g.Edge e = new g.Edge(n, n) e.touches(n) Jx final Graph g = new OnOffGraph final g.class.Node n = new g.class.Node final g.class.Edge e = new g.class.Edge(n, n) e.touches(n) Java 1.5 OnOffNode n = new OnOffNode OnOffEdge e = new OnOffEdge(n, n) e.touches(n)

Side-by-side can we...gbetaJxJava 1.5 use/understand mix n/e from diff. G worth the trouble family is defined by yes no kind of instance no kind of no container class yes no yes container class G = graph n=node e=edge

Download ppt "Family Polymorphism & Nested Inheritance Yevgen Voronenko."

Similar presentations