CS263: Runtime Systems Lecture: High-level language virtual machines

Size: px
Start display at page:

Download "CS263: Runtime Systems Lecture: High-level language virtual machines"

Transcription

1 CS263: Runtime Systems Lecture: High-level language virtual machines Today: A Review of Object-oriented features Chandra Krintz UCSB Computer Science Department

2 Virtual machines (VMs) Terminology Aka managed runtime environments (MREs) Aka [high-level] language runtime (runtime) Vernacular Java: JVMs,.Net: Common language runtime (CLR), Python/others: runtime

3 Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java bytecode cls2.class

4 Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent static compiler (Ex: g++) executable code linkable code linker executable code Java bytecode cls2.class

5 Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java Virtual Machine Translation from bytecode to native machine code executable code Java bytecode cls2.class Runtime environment program loading/verif. memory management thread/synchronization optimization

6 Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java Virtual Machine Interpreter and/or dynamic compiler executable code Java bytecode cls2.class Runtime environment program loading/verif. memory management thread/synchronization optimization

7 Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java Virtual Machine Interpreter and/or dynamic compiler executable code static compiler (Ex: g++) executable code linkable code linker executable code Java bytecode cls2.class Runtime environment program loading/verif. memory management thread/synchronization optimization

8 C# Architecture-independent Format called bytecode Dynamically loaded / executed by a common language runtime (e.g. mono) File.cs class cls1 { } class cls2 { } source compiler (mcs File.cs) cls1.exe MSIL bytecode architecture-independent architecture-dependent Common Language Runtime Translation from bytecode to native machine code executable code MSIL bytecode cls2.exe Runtime environment program loading/verif. memory management thread/synchronization optimization

9 Python Architecture-independent Format called bytecode Dynamically loaded / executed by a python runtime (e.g. cpython) File.py Non-class code class cls1: source compiler (pyc File.py) cls1.pyc Python bytecode Or source code! architecture-independent architecture-dependent Python Runtime Translation from bytecode to native machine code executable code Runtime environment program loading/verif. memory management thread/synchronization optimization

10 Python Architecture-independent Format called bytecode Dynamically loaded / executed by a python runtime (e.g. cpython) File.py Non-class code class cls1: source compiler (pyc File.py) cls1.pyc Python bytecode architecture-independent architecture-dependent High-Level Lang. Runtime Translation from bytecode to native machine code executable code Runtime environment program loading/verif. memory management thread/synchronization optimization

11 OO Review Things you should already know (study/review if not) Static vs dynamic typing Type-safety, memory safety; vs type-inference Class Object Class/object members (aka attributes) Fields (data) Methods (code) Static fields vs instance fields Static methods vs instances methods Inheritance Dynamic dispatch of virtual instance methods Dynamically typed OO language features

12 Static vs Dynamic Typing STATIC Variable Name Type is associated with variable and known at compile time (C, C++, Java, C#, all below) -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name Object is of Type Type Object is of Type Type is associated with value and not known until runtime when the code executes

13 Static vs Dynamic Typing STATIC Variable Name Type is associated with variable and known at compile time -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name runtime Object is of Type Type runtime Object is of Type Type is associated with value and not known until runtime when the code executes

14 Static vs Dynamic Typing STATIC Variable Name Type is associated with variable and known at compile time -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name runtime In the code, known ahead of runtime runtime Object Type is of Type Object is of Type Type is associated with value and not known until runtime when the code executes

15 Static vs Dynamic Typing Variable Name STATIC Type is associated with variable and known at compile time -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name Object is of Type Type Must match (be compatible) Type Safe if this and operations on types are checked (statically if possible, dynamically required for some operations) and legal according to the type system. A type system generally tries to guarantee that operations are not used with values for which that operation does not make sense. Object is of Type Type is associated with value and not known until runtime when the code executes Checked at runtime

16 Example Languages Statically typed, not type safe or memory safe C, C++ Type-safe, memory safe, statically typed Java, C# & the.net languages, Scala, ML, Go, Swift If you don t specify the type, the compiler can infer it in many cases Memory safe = there is no way to access a memory location except through an object reference Cannot walk through memory arbitrarily Cannot dereference an object that is NULL Dynamically typed (most are sandboxed) Python, Ruby, Lua Javascript, PHP Perl, OS shell scripts

17 Java Classes/Objects class MyMainClass { public static void main(string argv[]) { MyClass A = new MyClass(); } } Object = An instance of a class def. Class members Methods and fields Same for all objects of that class Declared using the static modifier class MyClass { int field1; int field2; static int field3; } void meth1 (int arg1) { } static void meth2(int arg1){ } MyClass() { } 18

18 Java Classes/Objects class MyMainClass { public static void main(string argv[]) { MyClass A = new MyClass(); } } Object = An instance of a class def. Class members Methods and fields Same for all objects of that class Declared using the static modifier class MyClass { int field1; int field2; static int field3; } void meth1 (int arg1) { } static void meth2(int arg1){ } MyClass() { } Instance members = non-static members (each object has own copy) Methods and fields Created & allocated when new is called Initialized when the constructor is called (MyClass() { } above) 19

19 Type-safe Object Conversions (Java/C#) A variable with a class type Can hold Objects of its own type or of subtypes of its own type Source compiler checks this statically (at compile time) Child c = new Parent(); // is not allowed class Parent { int field1; } class Child extends Parent { int field2; } Child c = new Child(); ((Parent)c).field1 = 2;// OK Child has Parent parts via inheritance c.field1 = 2; //also works (is the same as above in this case) 20

20 Inheritance and Static Fields & Methods Most languages (Java, C++, Python (fields only), others) Class/static fields and methods (Should be) accessed using the Class Name Parent.staticMethod(); Parent.staticField = 2; If accessed via an object, compiler uses the static (declared) or casted type of variable, & replaces the access with one like above. Parent obj = new Parent(); obj.staticfield = 2; //is same as above Members are inherited by subclasses: Child extends Parent System.err.println(Child.staticField); // output: 2

21 Inheritance and Static Fields & Methods Most languages (Java, C++, Python (fields only), others) Class/static fields and methods If Child has a field/method with the same name (e.g. staticfield) Both are available (Child hides parent s field): Child obj=new Child(); obj.staticfield = 2; Accessible via a cast ((Parent)obj).staticField = 4; or directly thru the parent s class name: Parent.staticField = 7; Hidden NOT overridden

22 Inheritance and Instance Fields Statically typed languages (Java, C++, C#, ) Instance fields An object instance is required for access Accessible from an object Within the object or in a lookup table referenced in the object Fields in parent class are inherited by subclass Those with same names are hidden by subclass but still available! Use casts or assign into a variable that has the type of the parent Child inherits from Parent Child obj = new Child(); obj.field1 = 7; //field1 is in both Parent/Child ((Parent)obj).field1 = 4; //field1 inher from Parent

23 Type-safe Object Conversions (Java/C#) A variable with a class type Can hold Objects of its own type or of subtypes of its own type Source compiler checks this statically (at compile time) Child c = new Parent(); // is not allowed Can be casted To a supertype The compiler checks compatibility at compile time To a subtype The compiler inserts runtime check to check the underlying object type when this code executes Checks that the underlying object type is 24 the one to which the cast is converting class Parent { int field1; } class Child extends Parent { int field1; } Parent p = Child c = ((Child)p).field1 = 7; ((Parent)c).field1 = 2;

24 Inheritance and Instance Fields Statically typed languages (Java, C++, C#, ) Instance fields An object instance is required for access Accessible from an object Within the object or in a lookup table referenced in the object Fields in parent class are inherited by subclass Those with same names are hidden by subclass but still available! Use casts or assign into a variable that has the type of the parent Child inherits from Parent Child obj = new Child(); obj.field1 = 7; //field1 is in both Parent/Child ((Parent)obj).field1 = 4; //field1 inher from Parent

25 Inheritance and Instance Fields Statically typed languages (Java, C++, C#, ) Instance fields An object instance is required for access Accessible from an object Within the object or in a lookup table referenced in the object Fields in parent class are inherited by subclass Those with same names are hidden by subclass but still available! Cast or assign into a variable that has the type of the parent Child inherits from Parent Parent pobj = getit(); Child obj = new Child(); pobj.field1 = 2; //field1 inher. from Parent obj.field1 = 7; //field1 is in both Parent/Child ((Child)pobj).field1 = 1; //error or which field1? ((Parent)obj).field1 = 4; //field1 inher from Parent //how does this work in Java? pobj = new Parent(); Hidden NOT overridden ((Child)pobj).field1 = 1; //what about this?

26 Inheritance and Instance Methods Most languages (Java, C++, Python, Others) Instance methods An object instance is required for access Accessible from object (lookup table referred to by object) Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Static dispatch Dynamic dispatch

27 Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Static dispatch - Make them like fields and have the child hide them (making them still accessible) we use the static/casted type determine which method to call C++,C# use this Parent obj = new Child(); obj.instancemethod(); //instancemethod is defined in both //calls which one? ((Child)obj).instanceMethod(); //calls which one?

28 Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Static dispatch - Make them like fields and have the child hide them (making them still accessible) we use the static/casted type determine which method to call C++,C# use this Parent obj = new Child(); obj.instancemethod(); //instancemethod is defined in both //calls Parent s ((Child)obj).instanceMethod(); //calls Child s

29 Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Dynamic dispatch Look up the underlying object type at runtime to determine which method to call All instance methods in Java and Python use this Instance methods marked virtual in C++ and C# use this Parent obj = new Child(); obj.instancemethod(); ((Parent)obj).instanceMethod(); //instancemethod is defined in both //calls which one? //calls which one?

30 Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Dynamic dispatch Look up the underlying object type at runtime to determine which method to call All instance methods in Java and Python use this Instance methods marked virtual in C++ and C# use this Parent obj = new Child(); obj.instancemethod(); ((Parent)obj).instanceMethod(); //instancemethod is defined in both //calls Child s //calls Child s

31 Dynamically Typed OO Languages Python, Ruby, Javascript, Lua, Attributes/properties (fields and methods) are defined on the fly upon first write/definition Object == unordered set of key-value pairs Lookup of attributes is by name for every access Most OO languages employ class-based inheritance Javascript and Self use prototype inheritance Prototypes define a set of properties; prototypes are objects Objects can inherit their properties from a prototype (ie other objects) And then change them (diverge from prototype) Hidden classes (or maps) are used by the runtime to track which properties an object has any (control) point in the executing program

CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department

CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department CS263: Runtime Systems Lecture: High-level language virtual machines Part 1 of 2 Chandra Krintz UCSB Computer Science Department Portable, Mobile, OO Execution Model Execution model embodied by recent

More information

CS 32. Lecture 1: oops

CS 32. Lecture 1: oops CS 32 Lecture 1: oops Textbooks Problem Solving in C++ (CS 16) Chapters 10-18 Data Structures with C++ (CS 24) Chapters 12-14 Reader SBPrinter at UCen Grading Labs 20% Programming Assignments 20% 3 thirdterm

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Atelier Java - J1. Marwan Burelle.  EPITA Première Année Cycle Ingénieur. marwan.burelle@lse.epita.fr http://wiki-prog.kh405.net Plan 1 2 Plan 3 4 Plan 1 2 3 4 A Bit of History JAVA was created in 1991 by James Gosling of SUN. The first public implementation (v1.0) in 1995.

More information

Overview. Elements of Programming Languages. Objects. Self-Reference

Overview. Elements of Programming Languages. Objects. Self-Reference Overview Elements of Programming Languages Lecture 10: James Cheney University of Edinburgh October 23, 2017 Last time: programming in the large Programs, packages/namespaces, importing Modules and interfaces

More information

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 10

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 10 CS24: INTRODUCTION TO COMPUTING SYSTEMS Spring 2018 Lecture 10 PREVIOUSLY: ARRAY ACCESS C doesn t provide a very safe programming environment Previous example: array bounds checking int a; int r[4]; int

More information

Lecture 02, Fall 2018 Friday September 7

Lecture 02, Fall 2018 Friday September 7 Anatomy of a class Oliver W. Layton CS231: Data Structures and Algorithms Lecture 02, Fall 2018 Friday September 7 Follow-up Python is also cross-platform. What s the advantage of Java? It s true: Python

More information

CS-202 Introduction to Object Oriented Programming

CS-202 Introduction to Object Oriented Programming CS-202 Introduction to Object Oriented Programming California State University, Los Angeles Computer Science Department Lecture III Inheritance and Polymorphism Introduction to Inheritance Introduction

More information

Overview. Elements of Programming Languages. Objects. Self-Reference

Overview. Elements of Programming Languages. Objects. Self-Reference Overview Elements of Programming Languages Lecture 11: James Cheney University of Edinburgh November 3, 2015 Last time: programming in the large Programs, packages/namespaces, importing Modules and interfaces

More information

What is Inheritance?

What is Inheritance? Inheritance 1 Agenda What is and Why Inheritance? How to derive a sub-class? Object class Constructor calling chain super keyword Overriding methods (most important) Hiding methods Hiding fields Type casting

More information

Dynamically-typed Languages. David Miller

Dynamically-typed Languages. David Miller Dynamically-typed Languages David Miller Dynamically-typed Language Everything is a value No type declarations Examples of dynamically-typed languages APL, Io, JavaScript, Lisp, Lua, Objective-C, Perl,

More information

Data Abstraction. Hwansoo Han

Data Abstraction. Hwansoo Han Data Abstraction Hwansoo Han Data Abstraction Data abstraction s roots can be found in Simula67 An abstract data type (ADT) is defined In terms of the operations that it supports (i.e., that can be performed

More information

Chapter 2: Java OOP I

Chapter 2: Java OOP I Chapter 2: Java OOP I Yang Wang wyang AT njnet.edu.cn Outline OO Concepts Class and Objects Package Field Method Construct and Initialization Access Control OO Concepts Object Oriented Methods Object An

More information

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington

CSE Lecture 3: Objects 2 September Nate Nystrom University of Texas at Arlington CSE 3302 Lecture 3: Objects 2 September 2010 Nate Nystrom University of Texas at Arlington Administration Out of town this afternoon thru Monday HW1 due next Thursday 9/9 Types Last time: strongly typed

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 10a Andrew Tolmach Portland State University 1994-2017 Object-oriented Programming Programs are structured in terms of objects: collections of variables

More information

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism Type Joseph Spring Discussion Languages and Type Type Checking Subtypes Type and Inheritance and 7COM1023 Programming Paradigms 1 2 Type Type denotes the kind of values that programs can manipulate: Simple

More information

CSE 401/M501 Compilers

CSE 401/M501 Compilers CSE 401/M501 Compilers Code Shape II Objects & Classes Hal Perkins Autumn 2018 UW CSE 401/M501 Autumn 2018 L-1 Administrivia Semantics/type check due next Thur. 11/15 How s it going? Reminder: if you want

More information

CS 251 Intermediate Programming Inheritance

CS 251 Intermediate Programming Inheritance CS 251 Intermediate Programming Inheritance Brooke Chenoweth University of New Mexico Spring 2018 Inheritance We don t inherit the earth from our parents, We only borrow it from our children. What is inheritance?

More information

CSE 431S Type Checking. Washington University Spring 2013

CSE 431S Type Checking. Washington University Spring 2013 CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks

More information

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

C++ Programming: Polymorphism

C++ Programming: Polymorphism C++ Programming: Polymorphism 2018 년도 2 학기 Instructor: Young-guk Ha Dept. of Computer Science & Engineering Contents Run-time binding in C++ Abstract base classes Run-time type identification 2 Function

More information

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information

Arrays Classes & Methods, Inheritance

Arrays Classes & Methods, Inheritance Course Name: Advanced Java Lecture 4 Topics to be covered Arrays Classes & Methods, Inheritance INTRODUCTION TO ARRAYS The following variable declarations each allocate enough storage to hold one value

More information

Object-Oriented Concepts

Object-Oriented Concepts JAC444 - Lecture 3 Object-Oriented Concepts Segment 2 Inheritance 1 Classes Segment 2 Inheritance In this segment you will be learning about: Inheritance Overriding Final Methods and Classes Implementing

More information

Object Model Comparisons

Object Model Comparisons Object Model Comparisons 1 Languages are designed, just like programs Someone decides what the language is for Someone decides what features it's going to have Can't really understand a language until

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018 Java + OOP CSC207 Winter 2018 1 Why OOP? Modularity: code can be written and maintained separately, and easily passed around the system Information-hiding: internal representation hidden from the outside

More information

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Week 7. Statically-typed OO languages: C++ Closer look at subtyping C++ & Subtyping Week 7 Statically-typed OO languages: C++ Closer look at subtyping Why talk about C++? C++ is an OO extension of C Efficiency and flexibility from C OO program organization from Simula

More information

Object Model. Object Oriented Programming Spring 2015

Object Model. Object Oriented Programming Spring 2015 Object Model Object Oriented Programming 236703 Spring 2015 Class Representation In Memory A class is an abstract entity, so why should it be represented in the runtime environment? Answer #1: Dynamic

More information

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level

CSE1720. General Info Continuation of Chapter 9 Read Chapter 10 for next week. Second level Third level Fourth level Fifth level CSE1720 Click to edit Master Week text 08, styles Lecture 13 Second level Third level Fourth level Fifth level Winter 2014! Thursday, Feb 27, 2014 1 General Info Continuation of Chapter 9 Read Chapter

More information

Topic 9: Type Checking

Topic 9: Type Checking Recommended Exercises and Readings Topic 9: Type Checking From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6 and

More information

Topic 9: Type Checking

Topic 9: Type Checking Topic 9: Type Checking 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6

More information

ECE 2400 / ENGRD 2140 Computer Systems Programming Course Overview

ECE 2400 / ENGRD 2140 Computer Systems Programming Course Overview ECE 2400 / ENGRD 2140 Computer Systems Programming Course Overview Christopher Batten School of Electrical and Computer Engineering Cornell University http://www.csl.cornell.edu/courses/ece2400 What is

More information

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018 Java + OOP CSC207 Winter 2018 1 Why OOP? Modularity: code can be written and maintained separately, and easily passed around the system Information-hiding: internal representation hidden from the outside

More information

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context Types 1 / 15 Outline Introduction Concepts and terminology The case for static typing Implementing a static type system Basic typing relations Adding context 2 / 15 Types and type errors Type: a set of

More information

Chapter 5 Object-Oriented Programming

Chapter 5 Object-Oriented Programming Chapter 5 Object-Oriented Programming Develop code that implements tight encapsulation, loose coupling, and high cohesion Develop code that demonstrates the use of polymorphism Develop code that declares

More information

Practice for Chapter 11

Practice for Chapter 11 Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing

More information

Principles of Object Oriented Programming. Lecture 4

Principles of Object Oriented Programming. Lecture 4 Principles of Object Oriented Programming Lecture 4 Object-Oriented Programming There are several concepts underlying OOP: Abstract Types (Classes) Encapsulation (or Information Hiding) Polymorphism Inheritance

More information

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE 1 SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ Java Programming Language Java Introduced in 1995 Object-oriented programming

More information

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden

More information

Lecture 4: Extending Classes. Concept

Lecture 4: Extending Classes. Concept Lecture 4: Extending Classes Concept Inheritance: you can create new classes that are built on existing classes. Through the way of inheritance, you can reuse the existing class s methods and fields, and

More information

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner.

HAS-A Relationship. Association is a relationship where all objects have their own lifecycle and there is no owner. HAS-A Relationship Association is a relationship where all objects have their own lifecycle and there is no owner. For example, teacher student Aggregation is a specialized form of association where all

More information

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete.

C++ without Classes. CMSC433, Fall 2001 Programming Language Technology and Paradigms. More C++ without Classes. Project 1. new/delete. CMSC433, Fall 2001 Programming Language Technology and Paradigms Adam Porter Sept. 4, 2001 C++ without Classes Don t need to say struct New libraries function overloading confusing link messages default

More information

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing

More information

Polymorphism. Agenda

Polymorphism. Agenda Polymorphism Lecture 11 Object-Oriented Programming Agenda Classes and Interfaces The Object Class Object References Primitive Assignment Reference Assignment Relationship Between Objects and Object References

More information

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Roadmap. Java: Assembly language: OS: Machine code: Computer system: Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp

More information

The Java Language Implementation

The Java Language Implementation CS 242 2012 The Java Language Implementation Reading Chapter 13, sections 13.4 and 13.5 Optimizing Dynamically-Typed Object-Oriented Languages With Polymorphic Inline Caches, pages 1 5. Outline Java virtual

More information

Design issues for objectoriented. languages. Objects-only "pure" language vs mixed. Are subclasses subtypes of the superclass?

Design issues for objectoriented. languages. Objects-only pure language vs mixed. Are subclasses subtypes of the superclass? Encapsulation Encapsulation grouping of subprograms and the data they manipulate Information hiding abstract data types type definition is hidden from the user variables of the type can be declared variables

More information

Chapter 9 Subprograms

Chapter 9 Subprograms Chapter 9 Subprograms We now explore the design of subprograms, including parameter-passing methods, local referencing environment, overloaded subprograms, generic subprograms, and the aliasing and problematic

More information

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1 Object-Oriented Languages and Object-Oriented Design Ghezzi&Jazayeri: OO Languages 1 What is an OO language? In Ada and Modula 2 one can define objects encapsulate a data structure and relevant operations

More information

COMP322 - Introduction to C++

COMP322 - Introduction to C++ COMP322 - Introduction to C++ Winter 2011 Lecture 08 - Inheritance continued School of Computer Science McGill University March 8, 2011 Last Time Single Inheritance Polymorphism: Static Binding vs Dynamic

More information

CS153: Compilers Lecture 11: Compiling Objects

CS153: Compilers Lecture 11: Compiling Objects CS153: Compilers Lecture 11: Compiling Objects Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 3 due today Project 4 out Due Thursday Oct 25 (16 days) Project 5 released

More information

Introduction to Java

Introduction to Java Introduction to Java Module 1: Getting started, Java Basics 22/01/2010 Prepared by Chris Panayiotou for EPL 233 1 Lab Objectives o Objective: Learn how to write, compile and execute HelloWorld.java Learn

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions

More information

Exercise: Singleton 1

Exercise: Singleton 1 Exercise: Singleton 1 In some situations, you may create the only instance of the class. 1 class mysingleton { 2 3 // Will be ready as soon as the class is loaded. 4 private static mysingleton Instance

More information

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017] 301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-12: Polymorphisms

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question) CS/B.TECH/CSE(New)/SEM-5/CS-504D/2013-14 2013 OBJECT ORIENTED PROGRAMMING Time Allotted : 3 Hours Full Marks : 70 The figures in the margin indicate full marks. Candidates are required to give their answers

More information

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8.

OOPs Concepts. 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. OOPs Concepts 1. Data Hiding 2. Encapsulation 3. Abstraction 4. Is-A Relationship 5. Method Signature 6. Polymorphism 7. Constructors 8. Type Casting Let us discuss them in detail: 1. Data Hiding: Every

More information

Lecture 1: Overview of Java

Lecture 1: Overview of Java Lecture 1: Overview of Java What is java? Developed by Sun Microsystems (James Gosling) A general-purpose object-oriented language Based on C/C++ Designed for easy Web/Internet applications Widespread

More information

Semantic Analysis. Lecture 9. February 7, 2018

Semantic Analysis. Lecture 9. February 7, 2018 Semantic Analysis Lecture 9 February 7, 2018 Midterm 1 Compiler Stages 12 / 14 COOL Programming 10 / 12 Regular Languages 26 / 30 Context-free Languages 17 / 21 Parsing 20 / 23 Extra Credit 4 / 6 Average

More information

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Java Inheritance Written by John Bell for CS 342, Spring 2018 Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources. Review Which of the following is true? A. Java classes may either

More information

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

More information

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University CS5000: Foundations of Programming Mingon Kang, PhD Computer Science, Kennesaw State University OOP Three main programming mechanisms that constitute object-oriented programming (OOP) Encapsulation Inheritance

More information

The Pyth Language. Administrivia

The Pyth Language. Administrivia Administrivia The Pyth Language Lecture 5 Please make sure you have registered your team, created SSH keys as indicated on the admin page, and also have electronically registered with us as well. Prof.

More information

Programming overview

Programming overview Programming overview Basic Java A Java program consists of: One or more classes A class contains one or more methods A method contains program statements Each class in a separate file MyClass defined in

More information

CSE 307: Principles of Programming Languages

CSE 307: Principles of Programming Languages 1 / 57 CSE 307: Principles of Programming Languages Course Review R. Sekar Course Topics Introduction and History Syntax Values and types Names, Scopes and Bindings Variables and Constants Expressions

More information

invokedynamic under the hood

invokedynamic under the hood Nadeesh T V ORACLE India Pvt Ltd 26 Aug 2016 Outline 1 JVM Languages 2 PreInvokedynamic 3 Invokedynamic 4 MethodHandle 5 Summary JVM Languages Languages which can run on Java Virtual Machine (JVM) Should

More information

CSE 421 Course Overview and Introduction to Java

CSE 421 Course Overview and Introduction to Java CSE 421 Course Overview and Introduction to Java Computer Science and Engineering College of Engineering The Ohio State University Lecture 1 Learning Objectives Knowledgeable in how sound software engineering

More information

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes

Inheritance. Benefits of Java s Inheritance. 1. Reusability of code 2. Code Sharing 3. Consistency in using an interface. Classes Inheritance Inheritance is the mechanism of deriving new class from old one, old class is knows as superclass and new class is known as subclass. The subclass inherits all of its instances variables and

More information

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5 Outline Name, Scope and Binding In Text: Chapter 5 Names Variable Binding Type bindings, type conversion Storage bindings and lifetime Scope Lifetime vs. Scope Referencing Environments N. Meng, S. Arthur

More information

Chapter 9. Subprograms

Chapter 9. Subprograms Chapter 9 Subprograms Chapter 9 Topics Introduction Fundamentals of Subprograms Design Issues for Subprograms Local Referencing Environments Parameter-Passing Methods Parameters That Are Subprograms Calling

More information

Binghamton University. CS-140 Fall Dynamic Types

Binghamton University. CS-140 Fall Dynamic Types Dynamic Types 1 Assignment to a subtype If public Duck extends Bird { Then, you may code:. } Bird bref; Duck quack = new Duck(); bref = quack; A subtype may be assigned where the supertype is expected

More information

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 9 Robert Grimm, New York University 1 Review Last week Modules 2 Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic

More information

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG)

SKILL AREA 304: Review Programming Language Concept. Computer Programming (YPG) SKILL AREA 304: Review Programming Language Concept Computer Programming (YPG) 304.1 Demonstrate an Understanding of Basic of Programming Language 304.1.1 Explain the purpose of computer program 304.1.2

More information

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University Names, Scopes, and Bindings CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Names, Scopes, and Bindings Names are identifiers (mnemonic character

More information

CS 231 Data Structures and Algorithms, Fall 2016

CS 231 Data Structures and Algorithms, Fall 2016 CS 231 Data Structures and Algorithms, Fall 2016 Dr. Bruce A. Maxwell Department of Computer Science Colby College Course Description Focuses on the common structures used to store data and the standard

More information

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java

CS/ENGRD 2110 FALL Lecture 2: Objects and classes in Java 1 CS/ENGRD 2110 FALL 2017 Lecture 2: Objects and classes in Java http://courses.cs.cornell.edu/cs2110 CMS VideoNote.com, PPT slides, DrJava 2 CMS. Visit course webpage, click Links, then CMS for 2110.

More information

CS260 Intro to Java & Android 03.Java Language Basics

CS260 Intro to Java & Android 03.Java Language Basics 03.Java Language Basics http://www.tutorialspoint.com/java/index.htm CS260 - Intro to Java & Android 1 What is the distinction between fields and variables? Java has the following kinds of variables: Instance

More information

CSC 1214: Object-Oriented Programming

CSC 1214: Object-Oriented Programming CSC 1214: Object-Oriented Programming J. Kizito Makerere University e-mail: jkizito@cis.mak.ac.ug www: http://serval.ug/~jona materials: http://serval.ug/~jona/materials/csc1214 e-learning environment:

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 17: Types and Type-Checking 25 Feb 08 CS 412/413 Spring 2008 Introduction to Compilers 1 What Are Types? Types describe the values possibly

More information

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University

Names, Scopes, and Bindings. CSE 307 Principles of Programming Languages Stony Brook University Names, Scopes, and Bindings CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Names, Scopes, and Bindings Names are identifiers (mnemonic character

More information

Chapter 9 Inheritance

Chapter 9 Inheritance Chapter 9 Inheritance I. Scott MacKenzie 1 Outline 2 1 What is Inheritance? Like parent/child relationships in life In Java, all classes except Object are child classes A child class inherits the attributes

More information

Object Oriented Programming. Java-Lecture 11 Polymorphism

Object Oriented Programming. Java-Lecture 11 Polymorphism Object Oriented Programming Java-Lecture 11 Polymorphism Abstract Classes and Methods There will be a situation where you want to develop a design of a class which is common to many classes. Abstract class

More information

Lecture 8: JavaScript

Lecture 8: JavaScript Lecture 8: JavaScript JavaScript introduction Examples Languages syntax and semantics Delegation vs. inheritance CS 242, Fall 2011, Lecture 8 1 What is JavaScript? JavaScript is a scripting language heavily

More information

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes

Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to. Inner classes Classes Types and Classes I II From predefined (simple) and user-defined (composite) types via Abstract data types Advanced oo concepts Specialization of behaviour? Multiple inheritance - alternatives to Inner

More information

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited CMSC 330: Organization of Programming Languages Type Systems, Names & Binding Topics Covered Thus Far Programming languages Syntax specification Regular expressions Context free grammars Implementation

More information

Introduction to Java. Handout-1d. cs402 - Spring

Introduction to Java. Handout-1d. cs402 - Spring Introduction to Java Handout-1d cs402 - Spring 2003 1 Methods (i) Method is the OOP name for function Must be declared always within a class optaccessqualifier returntype methodname ( optargumentlist )

More information

Delft-Java Link Translation Buffer

Delft-Java Link Translation Buffer Delft-Java Link Translation Buffer John Glossner 1,2 and Stamatis Vassiliadis 2 1 Lucent / Bell Labs Advanced DSP Architecture and Compiler Research Allentown, Pa glossner@lucent.com 2 Delft University

More information

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University Lecture 3 COMP1006/1406 (the Java course) Summer 2014 M. Jason Hinek Carleton University today s agenda assignments 1 (graded) & 2 3 (available now) & 4 (tomorrow) a quick look back primitive data types

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 12 Thomas Wies New York University Review Last lecture Modules Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic

More information

Subtyping (Dynamic Polymorphism)

Subtyping (Dynamic Polymorphism) Fall 2018 Subtyping (Dynamic Polymorphism) Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References PFPL - Chapter 24 Structural Subtyping - Chapter 27 Inheritance TAPL (pdf) - Chapter

More information

Implementing objects as in Javascript, Lua, 164 is expensive because object attributes are implemented as hashtable accesses:

Implementing objects as in Javascript, Lua, 164 is expensive because object attributes are implemented as hashtable accesses: Lectures Page 1 L18 Monday, November 23, 2009 10:22 PM Implementing objects as in Javascript, Lua, 164 is expensive because object attributes are implemented as hashtable accesses: x = { f=1 } x.f -->

More information