Objects and Classes Gul Agha CS 421 Spring 2006. 10/11/2001CS 322 Fall 20012 Characteristics of OOP Object-based  encapsulate state  provide interface.

Slides:



Advertisements
Similar presentations
Objectives Understand grammar of OOP Understand equivalent Java code Discuss different implementations of classes and objects.
Advertisements

Chapter 5: Objects and Classes class Point(Object): def __init__(x, y): self.x = initx self.y = inity def move(dx, dy): x += dx; y += dy; p = Point(10,
Lecture 10: Part 1: OO Issues CS 540 George Mason University.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
OOP: Inheritance By: Lamiaa Said.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Object Oriented Programming Chapter 7 Programming Languages by Ravi Sethi.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
CS 2511 Fall  Abstraction Abstract class Interfaces  Encapsulation Access Specifiers Data Hiding  Inheritance  Polymorphism.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Ruby: Multiple Inheritance, Interfaces, Mixins 1.
Cs784(Prasad)L123Assg1 Assignments. cs784(Prasad)L123Assg2 l-value vs. r-value Pascal/Ada: x := x + 1 C/Java: x = x + 1 l-value = location, address, reference,
EOPL3: Section 3.3 PROC and App B: SLLGEN
CISC6795: Spring Object-Oriented Programming: Polymorphism.
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
5.4.1: Implementation Method Invocation (define eval-expression (lambda (exp env) (cases expression exp... (method-app-exp (obj-exp method-name rands)
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
11 Chapter 11 Object-Oriented Databases Database Systems: Design, Implementation, and Management 4th Edition Peter Rob & Carlos Coronel.
JAVA WORKSHOP SESSION – 3 PRESENTED BY JAYA RAO MTech(CSE) NEWTON’S INSTITUTE OF ENGINEERING 1.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types Recall inductive BNF definition of binary trees: ::= | ( ) To write programs using.
Inheritance - Polymorphism ITI 1121 Nour El Kadri.
15440 Distributed Systems Recitation 1 Objected-Oriented Java Programming.
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
Objects & Dynamic Dispatch CSE 413 Autumn Plan We’ve learned a great deal about functional and object-oriented programming Now,  Look at semantics.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
CS451 - Lecture 2 1 CS451 Lecture 2: Introduction to Object Orientation Yugi Lee STB #555 (816) * Acknowledgement:
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
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
JAVA Programming (Session 4) “When you are willing to make sacrifices for a great cause, you will never be alone.” Instructor: รัฐภูมิ เถื่อนถนอม
Object Modeling THETOPPERSWAY.COM. Object Modelling Technique(OMT)  Building a model of an application domain and then adding implementation.
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
3.3 Conditional Evaluation --> if 1 then 2 else 3 2 Want to support if/then/else We'll represent false as 0, true as 1: Syntax (BNF): ::= if then else.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
ISBN Chapter 12 Support for Object-Oriented Programming.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Multiple Inheritance, Interfaces, Mixins 1.
Inheritance a subclass extends the functionality of a superclass a subclass inherits all the functionality of a superclass don't reinvent the wheel – "stand.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
Operational Semantics of Scheme
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Abstract Syntax cs7100 (Prasad) L7AST.
Topic: Classes and Objects
Sixth Lecture ArrayList Abstract Class and Interface
Sections Inheritance and Abstract Classes
Inheritance ITI1121 Nour El Kadri.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
Chapter 2: Data Abstraction 2
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
5.4.1: Implementation Method Invocation
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Abstract Syntax cs7100 (Prasad) L7AST.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
Presentation transcript:

Objects and Classes Gul Agha CS 421 Spring 2006

10/11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface and behavior to access state Class-based  classes to create/group objects Inheritance  subclasses to specialize classes Polymorphism

10/11/2001CS 322 Fall Object-Based Programming Objects partitioned into two parts:  consisting of fields  methods have access to the fields Calling a method is:  sending a message to the object with method name and arguments

10/11/2001CS 322 Fall Class-based Programming Classes provide:  structures that specify the fields and methods of each object. But not the state (i.e., contents of fields)  objects are created as an instance of the class.

10/11/2001CS 322 Fall Object-Oriented Programming Languages Object-based + Class-based + Inheritance +  allow definition of new classes by adding or modifying some of the methods (or fields) of an existing class. Polymorphism:  messages may be sent to objects of different classes.

10/11/2001CS 322 Fall Example class c1 extends object field i field j method initialize (x) … method countup (d) … method getstate ( ) …

10/11/2001CS 322 Fall Method Definitions method initialize (x) begin set i = x; set j = - (0, x) end

10/11/2001CS 322 Fall Method Definitions (contd.) method countup (d) begin set i = + (i, d); set j = - (j, d) end method getstate ( ) list (i, j)

10/11/2001CS 322 Fall Body let t1 = 0 t2 = 0 o1 = new c1 (3) in begin set t1 = send o1 getstate ( ); send o1 countup (2); set t2 = send o1 getstate ( ); list(t1, t2) end

10/11/2001CS 322 Fall Dynamic Dispatch Binary tree has two kinds of nodes  interior nodes  leaf nodes Send sum message to find the sum of the leaves of a node  node may be interior or leaf

10/11/2001CS 322 Fall Dynamic Dispatch Example class interior_node extends object field left field right method initialize (l, r) begin set left = l; set right = r end method sum ( ) + (send left sum ( ), send right sum ( ) )

10/11/2001CS 322 Fall Dynamic Dispatch Example (contd) class leaf_node extends object field value method initialize (v) set value = v method sum ( ) value

10/11/2001CS 322 Fall Dynamic Dispatch of sum let o1 = new interior_node ( new interior _node ( new leaf_node (3), new leaf_node (4)), new leaf_node (5)) in send o1 sum ( )

10/11/2001CS 322 Fall Inheritance Define new classes by incremental modification of already defined classes. Hierarchical classification of objects Terminology:  If c2 extends c1, c1 is the parent of c1.  Ancestor defined by transitive closure of parent relation. Each class has a single parent.

10/11/2001CS 322 Fall Classic Example of Inheritance class point extends object field x field y method initialize (initx, inity) … method move (dx, dy) … method get_location ( ) …

10/11/2001CS 322 Fall method initialize (initx, inity) begin set x = initx; set y = inity end method move (dx, dy) begin set x = +(x, dx); set y = +(y, dy) end method get_location ( ) list (x, y)

10/11/2001CS 322 Fall Subclass of Point class colorpoint extends point field color method set_color (c) set color = c method get_color ( ) color

10/11/2001CS 322 Fall let p = new point (3, 4) cp = new colorpoint (10, 20) in begin send p move (3, 4); send cp set_color (87) send cp move (10, 20) list (send p get_location( ), % returns (6 8) send cp get_location( ), % returns (20 40) send cp get_color ( ) )% returns 87 end

10/11/2001CS 322 Fall Properties of Inheritance Method definition in subclass may override method definition in class. Concept of self for dynamic dispatch Super to refer to superclass methods Static dispatch (static typing)

10/11/2001CS 322 Fall Class c1 extends object method initialize () 1 method m1 () 1 method m2 () 100 method m3 () send self m2 () Class c2 extends c1 method initialize () 1 method m2 () 2 Let o1 = new c1 () o2 = new c2 ()

10/11/2001CS 322 Fall An Object-Oriented Language Add objects and lists as expressed values Expressed Value = Number + ProcVal + Obj + List (Expressed Value) Denoted Value = Ref (Expressed Value) Classes are neither denotable nor expressible.

10/11/2001CS 322 Fall Abstract Syntax of Declarations  a-program (class-decls body)  a-class-decl (class-name super-name field-ids method-decls)  a-method-decl (method-name ids body)

10/11/2001CS 322 Fall Abstract Syntax of Expressions  new-object-exp (class-name rands)  method-app-exp (obj-exp method-name rands)  super-call-exp (method-name rands)

10/11/2001CS 322 Fall Evaluating Programs (define eval-program (lambda (pgm) (cases program pgm (a-program (c-decls exp) (elaborate-class-decls! c-decls) (eval-expression exp (empty-env))))))

10/11/2001CS 322 Fall Evaluating Class Declarations Implementation of (elaborate-class-decls! c-decls) must store the class declarations for later use in the body of the program We will describe four implementations later.

10/11/2001CS 322 Fall OO Language Implementations Each implementation must supply (elaborate-class-decls! c-decls) (object  class-name obj) (find-method-and-apply method-name (object  class-name obj) obj args ) (new-object class-name)

10/11/2001CS 322 Fall Evaluating Expressions for Object extensions (define eval-expression (lambda (exp env) (cases expression exp (lit-exp (datum) datum) (var-exp (id) (apply-env env id)) … (new-object-exp … ) (method-app-exp … ) (super-call-exp … ))))

10/11/2001CS 322 Fall Applying Method Expressions (method-app-exp (obj-exp method-name rands) (let ((args (eval-rands rands env)) (obj (eval-expression obj-exp env))) (find-method-and-apply method-name (object  class-name obj) obj args)))

10/11/2001CS 322 Fall Evaluating calls to Superclass methods Similar to ordinary method invocations except method is looked up in the superclass. (super-call-exp (method-name rands) (let ((args (eval-rands rands env)) (obj (apply-env env 'self))) (find-method-and-apply method-name (apply-env env '%super) obj args)))

10/11/2001CS 322 Fall Evaluating calls to Create Objects (new-object-exp (class-name rands) (let ((args (eval-rands rands env)) (obj (new-object class-name))) (find-method-and-apply 'initialize class-name obj args) obj ))

10/11/2001CS 322 Fall A Simple Implementation Represent classes and methods by their declarations. Represent an object as a list of parts.

10/11/2001CS 322 Fall Class Declarations Class declarations already contain information needed:  class’s name  immediate superclass’s name  field identifiers  method declarations Build repository of class declarations. Look up class name and return corresponding declaration when used.

10/11/2001CS 322 Fall Class Environment (define the-class-env '()) (define elaborate-class-decls! (lambda (c-decls) (set! the-class-env c-decls)))

10/11/2001CS 322 Fall Representing Objects Object is a list of parts One part corresponding to each class in the inheritance chain Each part has  class name  vector to hold corresponding state

10/11/2001CS 322 Fall Build Objects Build objects by  constructing a list of parts, given a class name.  leftmost is immediate class.  rightmost is the class named object (top of class hierarchy)

10/11/2001CS 322 Fall Part Datatype (define-datatype part part? (a-part (class-name symbol?) (fields vector?))) (define make-first-part (lambda (c-decl) (a-part (class-decl->class-name c-decl) (make-vector (length (class-decl  field-ids c-decl)) ))))

10/11/2001CS 322 Fall Creating Objects (define new-object (lambda (class-name) (if (eqv? class-name 'object) '( ) (let ((c-decl ( lookup-class class-name ))) (cons (make-first-part c-decl) (new-object (class-decl  super-name c-decl) ))))))

10/11/2001CS 322 Fall Generalize Accessors  to allow composition of accessors Use lookup-class when necessary (define class-name->method-decls (lambda (class-name) (class-decl->method-decls (lookup- class class-name))))

10/11/2001CS 322 Fall Other Accessors (define class-name->super-name (lambda (class-name) (class-decl->super-name (lookup-class class- name)))) (define object->class-name (lambda (parts) (part->class-name (car parts))))