Designing a Java Extension with Mixins Presented by: Yoav Gur

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

Inheritance Inheritance Reserved word protected Reserved word super
ITEC200 – Week03 Inheritance and Class Hierarchies.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 Chapter 12 More OOP, Interfaces, and Inner Classes.
Road Map Introduction to object oriented programming. Classes
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.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Encapsulation by Subprograms and Type Definitions
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
Subclasses. Inheritance class Animal { int row, column; // will be inherited private Model model; // private prevents inheritance Animal( ) {... } //
Abstract classes and Interfaces. Abstract classes.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
REFACTORING Lecture 4. Definition Refactoring is a process of changing the internal structure of the program, not affecting its external behavior and.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
CSM-Java Programming-I Spring,2005 Objects and Classes Overview Lesson - 1.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Implementation: Part 3 Software Construction Lecture 8.
Sun Certified Java Programmer, ©2004 Gary Lance, Chapter 5, page 1 Sun Certified Java 1.4 Programmer Chapter 5 Notes Gary Lance
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Programming in Java CSCI-2220 Object Oriented Programming.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Types in programming languages1 What are types, and why do we need them?
Introduction to Generics
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Object-Oriented Programming Chapter Chapter
 Objects versus Class  Three main concepts of OOP ◦ Encapsulation ◦ Inheritance ◦ Polymorphism  Method ◦ Parameterized ◦ Value-Returning.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Variations on Inheritance Object-Oriented Programming Spring
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
COP INTERMEDIATE JAVA Inheritance, Polymorphism, Interfaces.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
CSCE 240 – Intro to Software Engineering Lecture 3.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Review: Two Programming Paradigms
Chapter 9 Inheritance and Polymorphism
Java Programming Language
Polymorphism and access control
Interfaces.
Polymorphism Polymorphism
Java – Inheritance.
Java Inheritance.
CISC/CMPE320 - Prof. McLeod
Java Programming Language
Lecture 10 Concepts of Programming Languages
SPL – PS3 C++ Classes.
Presentation transcript:

Designing a Java Extension with Mixins Presented by: Yoav Gur

Definition A mixin is a parametric heir class, that can be instantiated on different classes to create different heirs, that extends those classes using the features defined in the mixin. Every mixin is applicable only on a predefined family of parent classes, which satisfy certain requirements defined by the mixin.

“Designing a Java Extension…” Must correspond to Java principles: Object oriented. Strongly typed. Allows overriding, overloading, and hiding. Dynamic binding of non-static methods. Static binding of fields and static methods.

“…with Mixins” A new notation must be added: A way to declare a mixin. A way to state requirements from the parent class to be extended. Presents two new preserved words: “mixin” and “inherited”.

A simple example – mixin declaration Use the new preserved word “mixin” to declare a new mixin. Use the new preserved word “inherited” to state requirements from the parent class. A legal parent class must supply the stated fields, and the exact stated methods. No constructors. can you think on a different logical requirements in regards to methods? mixin Undo{ inherited String getText(); inherited void setText(String s); String lastText; void setText(String s){ lastText = getText(); super.setText(s); } void undo(){ setText(lastText); }

A simple example – Mixin instantiation We will call “mixin instance” a class obtained by instantiating a mixin (not to be confused with an instance of a class). Constructors of the new mixin instance are to be defined in the curly brackets. class Textbox extends Component{ String text; … String getText() { … } void setText(String s) { … } } class TextboxWithUndo = Undo extends Textbox { }

Semantics – “The copy principle” “A class obtained by instansiating a mixin M on a parent class P should have the same behavior as a usual heir of P whose body contains a copy of all components defined in M” Intuition: It is possible to get the same effect of mixin instantiation in pure Java by defining many “hand-made” subclasses, which extend different parent classes and have the exact same body.

A mixin is a type So far, the presented extension allows avoiding code duplication by using mixins. But that’s not all: a mixin can be used as a type, and a mixin instance is a subtype of both the mixin and the parent class on which it has been instantiated.

About mixin as a type A mixin type M can be used in any place that a interface type can be used. It is possible to access inherited fields and invoke inherited methods on expressions of mixin types. A mixin can implement interfaces in exactly the same way a class does. An heir class, H = M extends P, is a subtype of both M and P, and therefore is implicitly convertible to both. As in interface types, a mixin type cannot be used for creating objects.

Static members A semantic question rises: should we count the number of inc() invoked from all mixin instances of M, or keep a separate counter for each mixin instance? “The copy principle” dictates the latter. Therefore, static members are not a part of the mixin type. (i.e. H.inc() is legal, but M.inc() is not legal). mixin M { static void inc() { counter++ ;} static int counter = 0; } class H = M extends Object { }

Abstract methods Declaring an inherited abstract method is a request for the parent class to declare, but not necessarily to implement, the method. The parent class can implement the method. It is also possible to redefine a method as abstract: mixin M { inherited abstract void f(); } mixin M { inherited void f(); abstract void f(); }

Ambiguity in field accesses in case of double mixin instantiation Which f will be 7? The copy principle doesn’t help here Arbitrary solution that corresponds the most to “the spirit of Java”: The field which is selected is determined by the dynamic type of m. Still: Jam compiler will produce a warning to make the user aware that field accesses through a reference of the mixin type have nonobvious semantics. mixin M { int f; } class Once = M extends Base{} class Twice = M extends Once{} class Test { void test(){ Once o = new Twice(); foo(o); } void foo(M m) { m.f = 7; } } class Twice has two fields named f, one is hidden by the other.

Translation into Java – Jam’s implementation inherited declarations in mixin M are translated to an interface Parent$M. A mixin M is translated to an interface M that extends Parent$M, with getters & setters for M’s fields. Access to M’s fields is translated to a call to the appropriate getter/setter. –Corresponds to dynamic binding of M’s fields in case of double instantiation. “class H = M extends P {}” is translated to “class H extends P implements M {…}” M’s body is copied to H’s body, according to the copy principle. Code duplication is only “delayed” until the translation to Java.

Implicit Constraints All constraints we saw so far were positive: –What must exist (abstract is an exception) There are also, implicit, negative specification –What must not be in the base class Three implicit such constraints: –Unexpected overriding: overriding a method which was not declared as inherited –Illegal overriding: not a full signature match –Ambiguous overloading: (roughly) a method in the mixin overloads a method in the base, which may cause surprises to the user Like positive constraints, depend only on mixin type (independent of implementation). –A modification of a method body does not affect instantiation correctness.

Unexpected Overriding Mixin M, method m M, class C, method m C The names of m M and m C are identical. Signatures are identical However, M does not declare m C as inherited. Example: mixin Undo on a class with a void undo() A Correct Java class, not what you expect. JAM produces an error message at –instantiation time Note: Unexpected overriding of fields and static methods is allowed!

Illegal Overriding Mixin M, method m M, class C, method m C The names of m M and m C are identical. Signatures are almost identical (same argument types), but: –Different return type –Different classification (static/instance) –Different throws clause JAM produces an error message at –instantiation time

Overloading and Ambiguity ( for now, forget about mixins ) Overloading: class C has several implementations of method m, all with different signature. Ambiguity: more than one signature matches a call site. –Which version should the compiler use for (new C()).m(…) Java has built-in rules for ambiguity resolution. There are cases when these rules fail. class A {} class B extends A{} class C { void m(B b) {} void m(A a) {} } (new C()).m(new B()) m(B b) will be chosen class C { void m(Integer i) {} void m(String s) {} } (new C()).m(null) No Solutio n

Java Ambiguity Resolution Tournament Characterizes a candidate –C: The declaring class –Pi: The i th parameter type vs. –Signs: C 1 ≥ C 2 ≡ “C 1 is equally or more specific then C 2 ” Pi 1 ≥ Pi 2 ≡ “P 1 is equally or more specific then P 2 ” – will win iff: C 1 ≥ C 2 AND P1 1 ≥ P1 2 AND … AND Pn 1 ≥ Pn 2 If there is more then one champion: –No Solution

Ambiguous Overloading with Mixins – Motivation (1) Mixin M, method m M, class C, method m C, mixin instance H –m M may be an inherited method An overloading relation between m M and m C M’s author, do not know that it will be attached to C, and therefore is unaware of the overloading conflict. Jam’s compiler can not know M’s dynamic type at compilation time. –Also unaware of overloading between m M and m C

Ambiguous Overloading with Mixins - Motivation (2) m C is inheritedm C is not inherited Call Site inside M M is not legal Error on call site inside M M is legal, H is not legal Error on generated Java code of H Call Site outside M M is legal, H is legal Error on call site outside M M is legal, H is legal Error on generated Java code outside M Assume: –Overloading relation between m M and m C –Ambiguous call to m M () either from inside M (this.m M ()) or from outside M (H.m M ()) Without the third constraint:

Ambiguous Overloading with Mixins Solution - Third implicit constraint: –m C is not inherited –The names of m M and m C are identical –Signatures are interfering Difference is only in parameter type of reference types –JAM produces an error message at instantiation time Assuming {boolean,int} are the only primitive types, avoids ambiguities entirely. Otherwise, interfering should be redefined.

What’s the Type of this ? mixin M { void g() { int i = A.f(this);} class H = M extends Object{} class H { void g() { int i = A.f(this); } mixin M class H However, by the copy principle (which is also the way JAM is implemented)… Problem: the type of “this” dictates ambiguity resolution rules Solution: “this” has type M, but it cannot be passed as an argument! It can be assigned to other variables, of explicit type M.

Limitations No parametric notation for the parent and heir. Some code can’t be generated by mixins: class P {} class H extends P { public H m() {return this;} } class P {} class H extends P { public P m() {return this;} } class P {} mixin M { public M m() {return this;} } class H = M extends P{} ≠ Adding such a notation would prevent using mixin as a type.

Translation into Java – Jam’s implementation (reminder) inherited declarations in mixin M are translated to an interface Parent$M. A mixin M is translated to an interface M that extends Parent$M, with getters & setters for M’s fields. Access to M’s fields is translated to a call to the appropriate getter/setter. –Corresponds to dynamic binding of M’s fields in case of double instantiation. “class H = M extends P {}” is translated to “class H extends P implements M {…}” M’s body is copied to H’s body, according to the copy principle. Code duplication is only “delayed” until the translation to Java.

Alternative translation into Java homogeneous implementation A mixin M is translated the same way as in Jam’s implementation. M’s methods which are not getters/setters are translated to static methods of a new class “BodyM”, with an additional parameter “M This”. Those methods in H are implemented by calls to the corresponding methods in BodyM, with “this” as the additional parameter. Some code duplication is avoided. The downside: what about private/protected members in M? Getters & setters must become public for BodyM to be able to use them. Other methods must become public in BodyM for H to be able to use them.

Conclusion (1) The two design principles of Jam: –The copy principle –A mixin name can be used as a type. A mixin instance is a subtype of both the mixin and the parent class. Static members are not a part of the mixin type. In case of double mixin instantiation, field accesses through a reference of the mixin type use dynamic binding.

Conclusion (2) Overloading resolution corresponds to Java’s “more specific” rule. Instantiation is illegal in cases of unexpected overriding, illegal overriding, or ambiguous overloading. Some legal instantiations may still generate an erroneous Java class. use of “this” as argument inside a mixin is forbidden. No parametric notation for the parent and the heir inside the mixin. There is a tradeoff in the translation to java: code duplication vs. loosing encapsulation.