Advanced Methods in Programming Languages (201-2-4411-01)
Notes Week 10 - Michael Elhadad
Object Oriented Languages
An object-oriented language is characterized by the following properties:
- A new type of value - objects - encapsulate behavior (methods) and state (fields)
- Classes group objects that have the same structure (methods and field list)
- Inheritance allows new classes to be derived from existing ones (shared structure)
- 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:
- Class definitions: include fields and methods.
- Classes have a constructor - used to initialize the state of objects upon creation.
- Messages can be sent to objects - to invoke methods.
- 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".
- 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.
- 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