 Flattening and Direct semantics  By example: Inheritance  Featherweight Jigsaw (FJig)  (nice and clean) surface syntax  Generalized inheritance.

Slides:



Advertisements
Similar presentations
Writing Modern C++ Marc Grégoire Software Architect April 3 rd 2012.
Advertisements

The C ++ Language BY Shery khan. The C++ Language Bjarne Stroupstrup, the language’s creator C++ was designed to provide Simula’s facilities for program.
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
Object Orientation Chapter SixteenModern Programming Languages, 2nd ed.1.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
ITEC200 – Week03 Inheritance and Class Hierarchies.
INTERPRETER Main Topics What is an Interpreter. Why should we learn about them.
Inheritance. Extending Classes It’s possible to create a class by using another as a starting point  i.e. Start with the original class then add methods,
AspectJ2EE/Clasa Israel Institute of Technology The Computer Science department Itay Maman.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
 By Wayne Cheng.  Introduction  Five Tenets  Terminology  The foundation of C++: Classes.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
IT PUTS THE ++ IN C++ Object Oriented Programming.
Intro to Generic Programming Templates and Vectors.
The Interpreter Pattern. Defining the Interpreter Intent Given a language, define a representation for its grammar along with an interpreter that uses.
Inheritance. Types of Inheritance Implementation inheritance means that a type derives from a base type, taking all the base type’s member fields and.
Multiple Choice Solutions True/False a c b e d   T F.
Programming Languages and Paradigms Object-Oriented Programming.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
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.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
UML2 Package Merge Usage scenarios and their effect on XMI and Java API interoperability Bran Selic, Jim Amsden, Kenn Hussey Oct, 2003.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
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.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
C# F 1 CSC 298 Object Oriented Programming (Part 1)
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Software Engineering Design & UML.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CS162 Week 1 Kyle Dewey. Overview Basic Introduction CS Accounts Scala survival guide.
Object-Oriented Programming Chapter Chapter
Object Oriented Programming
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Interfaces About Interfaces Interfaces and abstract classes provide more structured way to separate interface from implementation
ISBN Object-Oriented Programming Chapter Chapter
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Variations on Inheritance Object-Oriented Programming Spring
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 20 – C++ Subclasses and Inheritance.
Module 9: Operator overloading #1 2000/01Scientific Computing in OOCourse code 3C59 Module 9: Operator Overloading In this module we will cover Overloading.
Design Patterns: MORE Examples
OOP: Encapsulation &Abstraction
Jim Fawcett CSE681 – SW Modeling & Analysis Fall 2014
Templates.
Inheritance and Polymorphism
CSC 221: Computer Programming I Fall 2005
Object Oriented Analysis and Design
CSC 143 Inheritance.
Names, Binding, and Scope
Java Programming Language
Objects and Aspects: What we’ve seen so far
More Object-Oriented Programming
LECTURE 2: The Object Model
CIS 199 Final Review.
Building Java Programs
C++ Object Oriented 1.
Presentation transcript:

 Flattening and Direct semantics  By example: Inheritance  Featherweight Jigsaw (FJig)  (nice and clean) surface syntax  Generalized inheritance  I’ll gloss over its (ugly but equivalent) core syntax  Conclusions

 Please think how you’d explain inheritance  to a muggle 1 ( 1 here, indicates someone who’s never heard of OO)  Odds are you’re thinking of flattening semantics

class C1 { void m1() {…} } class C2 extends C1 { void m2() {…} } It’s as we wrote C2 like this class C2 { void m1() {…} void m2() {…} } What does this mean?

Extended Language  Giving semantics of some feature by translating it away  Programs translated in (equivalent) programs that don’t use the feature Language class C2 extends C1 { void m2() {…} } class C2 { void m1() {…} void m2() {…} }  Extension flattened into the vanilla language  Let’s see pros and cons

class C1 { void m1() {…} } class C2 extends C1 { void m2() {…} } class C3 extends C2 { void m3() {…} } class C1 { void m1() {…} } class C2 { void m1() {…} void m2() {…} } class C3 { void m1() {…} void m2() {…} void m3() {…} }

class C1 { void m1() {…} } class C2 { void m1() {…} void m2() {…} } class C3 { void m1() {…} void m2() {…} void m3() {…} }  Lookup as easy as it can get  No changes in the definition (the program changes, not lookup)  For instance, lookup(C2, …)= (inheritance has been flattened out)

class C1 { void m1() {…} } class C2 extends C1 { void m2() {…} } class C3 extends C2 { void m3() {…} } class C1 { void m1() {…} } class C2 { void m1() {…} void m2() {…} } class C3 { void m1() {…} void m2() {…} void m3() {…} } the binary depends on

 Intuitive: good tool for teaching/understanding  Been used to give the semantics of mixins, traits, … but  Poor implementation choice  Lot of compiling dependencies (unlike Java, like C++)  Code bloating

 Please think how you’d implement inheritance  Odds are you’re thinking of direct semantics

Extended Language class C2 extends C1 { void m2() {…} }  Classes can be defined in new ways  Since programs are not translated, good old method lookup can’t work anymore Language class C1 { void m1() {…} } We can compute lookup(C, m)=… For instance, lookup(C1, m1)=… lookup(C2, m1)=???

 Needs to know what extends means  For instance, lookup(C3, m2) starts from C3  Don’t find m2, so proceeds with C2  And finds m2 class C1 { void m1() {…} } class C2 extends C1 { void m2() {…} } class C3 extends C2 { void m3() {…} }

FlatteningDirect IntuitiveYESLess than the other Good old method lookup YESNO Is efficient?NOYES

 class C ClassExpression  ClassExpression ::= BasicClass | merge ClassExpression1, ClassExpression2 | rename N to N’ in ClassExpression | hide N in ClassExpression | …others… BasicClass ::= { …constr/fields/methods… }

 One constructor; takes any kind of parameters  Internal representation hidden from clients  A series of members: fields and methods  Members can be:  abstract  virtual  frozen  local no definition “someone” is expected to provide it there is a definition, but it can be later replaced there is a definition, that current clients will see forever (even if replaced) there is a definition, that no other object will ever see

 Nice per se  More than that, can encode:  standard inheritance  mixins  traits  … it’s a general framework for sw composition  Wouldn’t be wonderful to have  an intuitive (flattening) semantics and  a (rather complex but) efficient (direct) one?  Actually, no it wouldn’t  … unless they’re equivalent! And they are!

 class A { abstract void m(); frozen int answer() { return 41; } local int loc() { return 1; } }  class B { frozen int answer() { return 42; } virtual void m() { loc(); } local int loc() { return answer(); } }  class C merge (rename answer to wrongAnswer in A), B  …what’s new C().answer()? I’m “cheating” using  an extra-sugared syntax  primitive types and void

 class C merge (rename answer to wrongAnswer in A), B  class C merge (rename answer to wrongAnswer in { abstract void m(); frozen int answer() { return 41; } local int loc() { return 1; } } ), B

 class C merge { abstract void m(); frozen int wrongAnswer() { return 41; } local int loc() { return 1; } }, B

 class C merge { abstract void m(); frozen int wrongAnswer() { return 41; } local int loc() { return 1; } }, { frozen int answer() { return 42; } virtual void m() { loc(); } local int loc() { return answer(); } }

 class C merge { abstract void m(); frozen int wrongAnswer() { return 41; } local int loc() { return 1; } }, { frozen int answer() { return 42; } virtual void m() { loc’(); } local int loc’() { return answer(); } }

 class C { // equivalent flattened definition frozen int wrongAnswer() { return 41; } local int loc() { return 1; } frozen int answer() { return 42; } virtual void m() { loc’(); } local int loc’() { return answer(); } }  now we can easily see what new C().answer() is  and, quite obviously, it’s 42

class A { abstract void m(); frozen int answer() { return 41; } local int loc() { return 1; } } class C merge (rename answer to wrongAnswer in A), B  again, what does new C().answer() means?  what is lookup(C, answer)? class B { frozen int answer() { return 42; } virtual void m() { loc(); } local int loc() { return answer(); } }

class A { abstract void m(); frozen int answer() { return 41; } local int loc() { return 1; } } class C merge (rename answer to wrongAnswer in A), B class B { frozen int answer() { return 42;} virtual void m() { loc(); } local int loc() { return answer(); } } lookup(C, answer) = lookup(merge …, answer) = = lookup((rename…), answer) and non-deterministically = lookup(B, answer)

lookup(rename FromN to ToN in ClassExpression, N)=  failure if N=FromN  lookup(ClassExpression, FromN) if N=ToN  lookup(ClassExpression, N) otherwise In the example: lookup((rename answer to wrongAnswer in A), answer) fails, so lookup(C, answer) = … 2 choices … = lookup(B, answer)

lookup(B, answer) = lookup( ClassExpression, answer) where ClassExpression is the class expression of B, which is a base (flatten) class. So, we can conclude. Believe it or not, I’ve just scratched the surface: direct semantics is no picnic

 Flatten semantics: easy, intuitive  but “performs” poorly  Direct semantics can be (rather) complex  but it’s a good choice for implementation  FJig: general language for software composition  encompasses inheritance, mixin, traits….  Given both semantics and proved equivalent  Implemented interpreter as master-thesis:  Exploring the equivalence for feature requiring static types (e.g. overloading and static binding)  Investigating smart implementation techniques