1 Types Object Oriented Programming 236703 Spring 2007.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Polymorphism. The term polymorphism literally means "having many forms" A polymorphic reference is a variable that can refer to different types of objects.
Generic programming in Java
Generics. DCS – SWC 2 Generics In many situations, we want a certain functionality to work for a variety of types Typical example: we want to be able.
UMBC 1 Static and Dynamic Behavior CMSC 432 Shon Vick.
Improving structure with inheritance
Java Generics.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
1 Generics, Type Safety, and Dynamic Data Structures.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Encapsulation by Subprograms and Type Definitions
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
Static and Dynamic Behavior Fall 2005 OOPD John Anthony.
Run-time Environment and Program Organization
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Interfaces besides classes, Java recognizes another type, an interface interface is used to completely shield off all implementation from the programmer.
Generic Java 21/ What is generics? To be able to assign type variables to a class These variables are not bound to any specific type until the.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Cs2220: Engineering Software Class 11: Subtyping and Inheritance Fall 2010 University of Virginia David Evans.
Question of the Day  Write valid mathematical equation using: one addition operator (‘+’) one equality operator (‘=’)  Should have equal values.
16 - Generics. 2 NOEA2009Java-kursus – Generics ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
Java Implementation: Part 3 Software Construction Lecture 8.
March 12, ICE 1341 – Programming Languages (Lecture #6) In-Young Ko Programming Languages (ICE 1341) Lecture #6 Programming Languages (ICE 1341)
The Java Programming Language
Generics in.NET and C# Generics. ”Generic” Programming in C#/Java (as it was until Summer 2005) All classes inherit from Object So we can apply polymorphism.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
1 Java: AP Curriculum Focus and Java Subset Alyce Brady.
CSSE501 Object-Oriented Development. Chapter 11: Static and Dynamic Behavior  In this chapter we will examine the differences between static and dynamic.
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
Constructors CMSC 202. Object Creation Objects are created by using the operator new in statements such as… The following expression invokes a special.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Copyright © Curt Hill Generic Classes Template Classes or Container Classes.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
ECE122 Feb. 22, Any question on Vehicle sample code?
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Types in programming languages1 What are types, and why do we need them?
Lecture 4 Generic programming Advanced Java Programming 1 dr hab. Szymon Grabowski dr inż. Wojciech Bieniecki
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
Polymorphism (generics) CSE 331 University of Washington.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
List Interface and Linked List Mrs. Furman March 25, 2010.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
C H A P T E R T H R E E Type Systems and Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Interfaces & Sub-types Weiss sec Scenario Instructor says: “Implement a class IntegerMath with two methods pow and fact with the following signatures:
JAVA GENERICS Lecture 16 CS2110 – Spring 2016 Photo credit: Andrew Kennedy.
Improving structure with inheritance Main concepts to be covered Inheritance Subtyping Substitution Polymorphic variables © 2017 Pearson Education,
Polymorphism in Methods
Sixth Lecture ArrayList Abstract Class and Interface
CS 326 Programming Languages, Concepts and Implementation
Java Generics.
Data Types.
עקרונות תכנות מונחה עצמים תרגול 21: Generics
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Generic programming in Java
Unit 3 Test: Friday.
UNIT V Run Time Environments.
1.4 ทบทวน JAVA OO Programming Concepts Declaring and Creating Objects
Subtype Substitution Principle
Presentation transcript:

1 Types Object Oriented Programming Spring 2007

2 Values 5 Color.RED "abc" new String[20] new Scanner("c:/temp/in.txt")‏c:/temp/in.txt new Properties()‏

3 Supported Operations int x = 5; Color.RED == null "abc".equalsIgnoreCase("ABC")‏ (new String[20])[0] new Scanner("c:/temp/in.txt").next()‏c:/temp/in.txt new Properites(0).getProperty("a")‏

4 Unsupported Operations int x = 5.9; Color.RED.equals(5,5)‏ "abc".remove(0)‏ (new String[20])[0][0] = "xyz"; new Scanner("c:/temp/in.txt") = nullc:/temp/in.txt Integer.parseInt(Properites(0))‏

5 Dynamic Typing The dynamic typing approach:  Compile-time: Allow all operations  Run-time: Before carrying out an operation, check that it is supported by the value Requirement:  A run-time data structure that determines the “support” question Consequences:  Assignments to variables never fail  No need to define types (of variables, return types, etc.)‏  Flexibility  Run-time errors if we are not careful

6 Static Typing The static typing approach:  Compile-time: Check that operations are correct  Run-time: Just carry out the operation The idea: The compiler “simulates” the evaluation of expressions  Compile-time computation of the type of the expression Corresponds to the run-time computation of the result of the expression If a type cannot be computed => Compilation error  The computed type of subexpressions is used in computing the type of bigger expressions

7 Static Typing (cont.)‏ Requirements  Variables have types  Methods have return types  Assignments are checked by the compiler  Message sends are checked by the compiler Consequences  More type errors are detected at compile-time  Programmer must type more text  Not as flexible: “You can't always do what you want”  Invocation of an action is faster But this factor becomes less and less important...

8 Supported? Yes/No? DT = Dynamic Typing; ST = Static Typing LinkedList w = new LinkedList(); w.addFirst("1"); // DT ST List x = new LinkedList(); x.addFirst("1"); // DT ST List y = new ArrayList(); y.addFirst("1"); // DT ST LinkedList z = new ArrayList(); // ST z.addFirst("1"); // DT interface List { void add(Object o); } class ArrayList implements List { void add(Object o) {... } } class LinkedList implements List { void addFirst(Object o) {... } void add(Object o) {... } }

9 What is a Type? Theoretically a type is defined as a set of values A more practical definition: A function that maps a value to either “yes”/”no”  The type is the set of all sources that are mapped to “yes” The compiler usually represents types as a directed graph  Two types of nodes: Messages, Types  Edges are labeled (ordinal numbers)‏  A type node has outgoing edges to all messages it can receive  A message node has outgoing edges to its return type, types of its parameters

10 Type Checking: Statically Typed Language Occurs when the compiler encounters an assignment  (Parameter passing is actually a sequence of assignments) void f(F f) { R r = f; // <- Type checking... } The idea: Compare the set of messages of F, R In Java:  A subtype shares messages with its supertypes  The sets are compared for inclusion (not equality)‏

11 Subtyping Schemes Nominal subtyping: A type Y shares messages with X if Y declares X as a super type  A fixed set of super types Structural conformance: A type Y shares messages with X if the messages have the same signature  One node for all messages of with the same signature  Regardless of where the messages are defined  An unlimited set of super types  This is actually a recursive definition

12 Nominal vs. Structural N = Nominal; S = Structural public interface R1 { public void a(); } public interface R2 { public void a(); } public interface F extends R1 { public void b(); } void g(F f) { R1 r1 = f; // N: S: R2 r2 = f; // N: S:... }

13 Typing Holes Recall: A type system should prevent us from applying an operation to a value that does not support it Is it really so? public class A { void f() {... } }.. void g(A a) { a.f(); } What values (of the variable a ) will not support the operation a.f() ?

14 Typing Strength Strong typing:  An unbreakable association between a value and its type Weak typing:  One can change the value-type association Modern languages: Strong typing  With either static or dynamic type checking