Family Polymorphism & Nested Inheritance Yevgen Voronenko.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Practice Problems: The Composition of Functions Work problems on your own first. Then check with answers in the following slides. If the answers don’t.
Exercise 1 Generics and Assignments. Language with Generics and Lots of Type Annotations Simple language with this syntax types:T ::= Int | Bool | T =>
Polymorphism. 3 Fundamental Properties of OO 1. Encapsulation 2. Polymorphism 3. Inheritance These are the 3 building blocks of object-oriented design.
Control Flow Analysis. Construct representations for the structure of flow-of-control of programs Control flow graphs represent the structure of flow-of-control.
OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.
ACM/JETT Workshop - August 4-5, :Inheritance and Interfaces.
Chapter 9 – Inheritance Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics (inheritance review + Java generics)
1 Lecture 4 Topics –Problem solving Subroutine Theme –REC language class The class of solvable problems Closure properties.
1 Lecture 3 Inheritance. 2 A class that is inherited is called superclass The class that inherits is called subclass A subclass is a specialized version.
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
CPSC150 Abstract Classes and Interfaces Chapter 10.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.

Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
Secure Web Applications via Automatic Partitioning Stephen Chong, Jed Liu, Andrew C. Meyers, Xin Qi, K. Vikram, Lantian Zheng, Xin Zheng. Cornell University.
Inheritance. Introduction Inheritance is one of the cornerstones of object-oriented programming because it allows the creation of hierarchical classifications.
Core Java: Essential Features 08/05/2015 Kien Tran.
Family Polymorphism 590P Seminar February 16, 2005.
Alg. I Practice. |x+4|+ 3 = 17 |x+4|= 14 or x+4 = -14 x+4 = 14 x = 10or x = -18.
Java Objects and Classes. Overview n Creating objects that belong to the classes in the standard Java library n Creating your own classes.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Polymorphism. 3 main programming mechanisms that constitute OOP: 1. Encapsulation 2. Inheritance 3. Polymorphism.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Verificare şi Validarea Sistemelor Soft Tem ă Laborator 1 ESC/Java2 Extended Static Checker for Java Dat ă primire laborator: Lab 1 Dat ă predare laborator:
Polymorphism (generics) CSE 331 University of Washington.
COMP Inheritance Basics Yi Hong June 09, 2015.
OO terminology & objectives §Encapsulation l don’t touch my private data l get/set it using my public/package methods §Inheritance l a parent provides.
Do Now Solve and graph. – 2k – 2 < – 12 and 3k – 3 ≤ 21.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
INF3110 Group 2 EXAM 2013 SOLUTIONS AND HINTS. But first, an example of compile-time and run-time type checking Imagine we have the following code. What.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Notes Over 1.6 Solving an Inequality with a Variable on One Side Solve the inequality. Then graph your solution. l l l
Family Polymorphism by Erik Ernst. Outline Class families Class families The “ OnOffGraph ” example The “ OnOffGraph ” example Traditional polymorphismTraditional.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Jeopardy Q $100 Q $100 Q $100 Q $100 Q $100 Q $200 Q $200 Q $200
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Class Inheritance Part II: Overriding and Polymorphism Corresponds with Chapter 10.
Dart Omar Ansari, Kenneth Jones, Isaac Wilder. 1. Overview l Object Oriented Language l Developed By Google l Primarily used for building websites, servers,
Polymorphism in Methods
Modern Programming Tools And Techniques-I
Polymorphism.
Graphing a System of Inequalities
Inheritance and Polymorphism
Behavioral Design Patterns
5 Construction 4 Line Perpendicular to a Given Line l, Passing Through a Given Point on l Method 1: solution Using a Set Square Line up one side of er.
Nested class.
BACK SOLUTION:
Object Oriented Programming
Variables as Remote Control
Building Java Programs
CS240: Advanced Programming Concepts
תוכנה 1 תרגול מספר 11: Static vs. Dynamic Binding
LL(1) Parser Generators
Advanced Java Topics Chapter 9
Week 3 Object-based Programming: Classes and Objects
Abstract Classes Page
Advanced Programming in Java
Chapter 9 Carrano Chapter 10 Small Java

Chapter 14 Abstract Classes and Interfaces
Object Oriented Programming
3(9z + 4) > 35z – 4 Original problem. Solve each inequality. Then graph the solution set on the number line. 3(9z + 4) > 35z – 4 Original problem.
Line Graphs.
Types of Errors And Error Analysis.
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