Advanced Methods in Programming Languages (201-2-4411-01)
Notes Week 10 - Michael Elhadad
previous class main page next class

Object Oriented Languages

An object-oriented language is characterized by the following properties:
  1. A new type of value - objects - encapsulate behavior (methods) and state (fields)
  2. Classes group objects that have the same structure (methods and field list)
  3. Inheritance allows new classes to be derived from existing ones (shared structure)
  4. Polymorphism allows messages to be sent to objects of different classes

A Sample Object-Oriented Language

The language is an extension of the standard language we have investigated so far. The extensions include:
  1. Class definitions: include fields and methods.
  2. Classes have a constructor - used to initialize the state of objects upon creation.
  3. Messages can be sent to objects - to invoke methods.
  4. Single inheritance: classes extend a base-class. Each class knows its super-class. The class hierarchy is rooted in a single root class called "object".
  5. Inheritance: when defining a sub-class, one can shadow the field of a parent class, or shadow the definition of a method of a parent class.
  6. One can send messages to the super (parent) class of an object.
The syntax of the language introduces the following new expression types:
<program> ::= {<class-decl>}* <expression>
              program (class-decls body)

<class-decl> ::= class <identifier> extends <identifier> {field <identifier>}* {<method-decl>}*
                 class-decl (class-name super-name field-ids method-decls)

<method-decl> ::= method <identifier> ({<identifier>}*) <expression>
                  method-decl (method-name ids body)

<expression> ::= new <identifier> ({<identifier>}*)
                 new-object-exp (class-name rands)

<expression> ::= send <expression> <identifier> ({<expression>}*)
                 method-app-exp (obj-exp method-name rands)

<expression> ::= super <identifier> ({<expression>}*)
                 super-call-exp (method-name rands)
An interpreter for this language is implemented in this file.
Last modified May 25, 2003 Michael Elhadad