1 / 14

Family Polymorphism & Nested Inheritance

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... } }

pavel
Download Presentation

Family Polymorphism & Nested Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Family Polymorphism &Nested Inheritance Yevgen Voronenko

  2. 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

  3. What We Have Node Edge OnOffNode OnOffEdge

  4. What We Want Node Edge Families OnOffNode OnOffEdge

  5. 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

  6. Solution 2: gbeta Compile Time Graph Node Edge NotFamilies OnOffGraph Node Edge Runtime g1 : Graph Family Node Edge

  7. 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 Graphg2 = 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);  ??

  8. Solution 3: Jx Compile Time Graph Node Edge Families OnOffGraph Node Edge 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);

  9. Jx “problem” A B B2 A2 B B2

  10. 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

  11. 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)

  12. 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)

  13. 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)

  14. Side-by-side G = graph n=node e=edge

More Related