Case Study: Java - Principles of Programming Languages - Slides | CSCI 3155, Study notes of Programming Languages

Material Type: Notes; Class: Principles of Programming Languages; Subject: Computer Science; University: University of Colorado - Boulder; Term: Spring 2001;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-a65
koofers-user-a65 🇺🇸

10 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Case study: Java
Amer Diwan
Java
Some similarities to Modula-3:
Strongly typed
Single inheritance O-O model
Garbage collection
Exceptions
Some differences from Modula-3:
C family syntax
Name type equality
No UNSAFE/Untraced
Separate interface and implementation inheritance
Bigger language definition (~ 400 pages excl. libs).
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download Case Study: Java - Principles of Programming Languages - Slides | CSCI 3155 and more Study notes Programming Languages in PDF only on Docsity!

Case study: Java

Amer Diwan

Java

  • Some similarities to Modula-3:
    • Strongly typed
    • Single inheritance O-O model
    • Garbage collection
    • Exceptions
  • Some differences from Modula-3:
    • C family syntax
    • Name type equality
    • No UNSAFE/Untraced
    • Separate interface and implementation inheritance
    • Bigger language definition (~ 400 pages excl. libs).

Reference types

  • Class: Implements one or more (explicit or implicit) interfaces
  • Array: these are objects too
  • Interface: Lists a set of methods that all implementations of the interface must have

Classes

  • Single inheritance like Modula-
  • Public, protected, private members like C++
  • Classes may be final: will not be subclassed
    • Has implications for implementation AND
    • Typing rules!!
  • Class “Object” is the top of the inheritance hierarchy

A solution from the client

  • Object a[]; a[0] = new Set; a[1] = new SortedList;
  • Problem: cannot do a[i].print()
  • Casting will be very awkward too!

Java’s solution: interfaces

  • Interfaces are like “fully abstract” classes
    • No code: just method declarations and constants
  • Interfaces may use multiple inheritance

Example: Interface and Classes

  • (From John Mitchell’s slides)

interface Shape { public float center() public void rotate(float degrees) } interface Drawable { public void setColor(Color c) public void draw() } class Circle implements Shape, Drawable { ... }

Using Java’s interfaces

  • interface HasAPrint { void print(); } class Set extends implements HasAPrint { void print() {...} }; class SortedList implements HasAPrint { void print() {…}}; HasAPrint a[]; a[0] = new Set; a[1] = new SortedList;
  • Clients only see interfaces and not implementation

Type equality

  • Unlike Modula-3, name equality, except:
    • Array types A and B are the same if they have the same element type
    • (Array index type is not important since all arrays are dynamically allocated and have a compile-time “unknown” index type)

Type compatibility in Java

  • Java expresses type compatibility in terms of narrowing and widening conversions

S

V

<: T

Narrowing

Widening

Intuition behind conversions

  • Widening
    • Subtype to supertype. Always legal
  • Narrowing
    • Supertype to subtype. May or may not be legal
    • Interface type to class (and vice versa). May or may not be legal
  • I’ll focus on reference conversions in this lecture

Conversions

  • Narrowing and widening casts defined for both primitive and reference types - Widening casts are applied implicitly or explicitly - Narrowing casts must be explicitly applied

Narrowing conversions

  • S to T if
    • S is superclass of T
    • S is a class, T is an interface, S is not a final class
    • S is ‘Object’ T is any array or interface type
    • S is an interface, T is not a final class
    • S is interface, T is final class , and T implements S

Narrowing conversions (cont.)

  • S and T are interfaces, but S is not a subinterface of T and they don’t declare incompatible methods
  • S and T are array types and there is a narrowing conversion from their element types
  • Narrowing conversions require run-time type tests
  • What are the tests involved in the last narrowing rule?

Examples

  • interface I {...} interface J {...} class S extends T implements I {...}
  • S to T?
  • T to S?
  • S[] to T[]?
  • T[] to S[]?
  • S to I?
  • S to J?

Assignment rules

  • a: A = b: B is allowed when
    • B = A
    • B can be converted to A using widening conversions (i.e., B <: A)
    • For primitives, sometimes a narrowing conversion is allowed

Summary

  • Type safe: unchecked type errors cannot happen at run-time - Particular important given Java’s target market
  • Usually opts for “clean” over “fast”
  • Mostly clean type system but perhaps not as elegant as that of M-
  • Separates implementation from interface

Next topic: Case study (Smalltalk)

  • Readings:
    • Scott (Sections 10.6.1)