CS 334
Programming Languages
Spring 2002

Lecture 16


Subtypes

Lecture material on subtypes can be found in Chapter 5 of my new book, Foundations of Object-Oriented Languages: Types and Semantics, by Kim Bruce (2002 by MIT Press). The relevant material for this lecture is included in section 5.1. Other material will be relevant when we get to object-oriented languages, next. I will not bother to put detailed lecture notes on-line because the chapter covers the same material (and examples!).

Discussed subtyping for:

Example typing problems from Component Pascal (discovered at end of December):
 MODULE Foo;
 TYPE
 base = RECORD 
    x : INTEGER;
 END;

 derived1 = RECORD(base)
    y : INTEGER;
 END;

 derived2 = RECORD(base)
    z : INTEGER;
 END;
 
 VAR
    b : POINTER to base;
 
 PROCEDURE bar(OUT a1 : POINTER TO derived1; OUT a2 : POINTER TO derived2);
 BEGIN
    NEW(a2);
    NEW(a1);
    a2^.z := 42;
    END bar;
 
 BEGIN
    bar(b, b);
 END Foo.
 
The extra information needed to understand this is that OUT parameters in Component Pascal are passed by reference! There is no problem if OUT parameters are passed by copy (i.e., call by result).

Object-oriented programming languages

Roots in languages supporting ADT's

Biggest loss in moving from FORTRAN to Pascal is lack of support for modules with persistent local data.

Clu, Ada, and Modula 2 attempted to remedy this by adding clusters, packages, and modules.

In Ada & Modula 2, objects (i.e. packages, and modules) were late additions to an earlier paradigm (Pascal-like)

Called object-based languages.

Goals:

Stepping back a bit further - support development of high-quality software.

Qualities Desired in Software

ADT languages provide reasonable support for all but extensibility (in particular if want minor extensions - but rest is same), some limitations on reuseability.

Object-oriented languages are an attempt to make progress toward these goals.

A programming language is object-oriented if:

  1. It supports objects that are data abstractions (like Modula 2, Ada).

  2. All objects have an associated object type (often called classes). (Bit different from Modula 2, Ada).

  3. Classes may inherit attributes from superclasses. (Very different from Modula 2, Ada)

  4. Computations proceed by sending messages to objects.

  5. Routines may be applied to objects which are variants of those they are designed to be applied to (subtype polymorphism).

  6. Support dynamic method invocation (will be explained later )

Simula 67 first object-oriented language - designed for discrete simulations.

Up until late '80's, Smalltalk best-known - Alan Kay at Xerox.

Gone through Smalltalk-72,-74,-76,-78,-80.

C++, object-oriented extensions to Pascal, C, LISP, etc.

One of nicest is Eiffel - discuss later (See Meyer's Object-Oriented Software Construction). Also Sather (public-domain variant of Eiffel). Of course Java is now becoming the most popular (and one of best).

Main idea of object-oriented programming:

Independent objects cooperate to perform a computation by sending messages (requests for action) to each other.

Object-oriented programming:

Object-oriented languages built around following concepts:

Object
like "internal representation of abstract data types - all data encapsulated in objects - first class!

Message
request for object to carry out one of its operations.

Class
template for set of objects - similar to type

Instance
object described by a class

Method
operation associated with an object - specified in its class.

Subtype
A subtype of B (A <: B) if A represents a specialization of B (e.g., cars <: vehicles, ColorPoint <: Point) - An element of a subtype can be used in any context in which an element of the supertype would work.

Subclass
An incremental modification or extension of a class and its methods. Methods not changed are inherited.

In more detail:

Objects are internal data abstractions - only accessible to outer world through associated procedures

Object types

Classes

Most current OOL's identify object types and classes (in particular subtypes and subclasses).

See later this can lead to holes in typing system and/or restrictions in expressibility.

In typical object-oriented programming language, everything is an object.

Abstraction preserved since no object can make changes to the internal state of another object - though some languages don't enforce this - just send messages using methods in the public interface.

Java

Java is an extension of C. Originally known as Oak, designed to program your toaster. Gained popularity when it provided support for "Applets", programs that could be embedded in web pages. (Sun's HotJava browser was the first to support applets.)

Three factors worked toward the sudden popularity of Java:

  1. Web applications.
  2. Use of C syntax.
  3. Growing frustration with C++.
The reasons that should have contributed to the rise of Java:
  1. Clean and (relatively) simple language design

  2. Use of garbage collection

  3. Built-in support for event-driven programming and concurrency.


Back to:
  • CS 334 home page
  • Kim Bruce's home page
  • CS Department home page
  • kim@cs.williams.edu