12 Java Case Study Essay

Submitted By Radhika-P
Words: 898
Pages: 4

Case study: Java
Amer Diwan

• Some similarities to Modula-3:

Strongly typed
Single inheritance O-O model
Garbage collection

• 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

• Single inheritance like Modula-3
• 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 2

Problem with class inheritance
• class HasAPrint { void print() {...} }; class Set extends HasAPrint { void print() {...} }; class SortedList extends HasAPrint { void print() {…}};
T a[100]; a[0] = new Set; a[1] = new SortedList;
• Let’s change the implementation of SortedList
• class SortedList extends Collection { void print() {…}}
• What’s the problem here?

Analysis of the problem
• Programmer used inheritance as a implementation reuse mechanism
• Client perceived inheritance as a subtyping mechanism 3

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


Complexity with interfaces
• interface Car { public void drive(unsigned speed)} interface Boat { public void drive(unsigned speed) } class AmphibiousVehicle implements Car, Boat {

• AmphibiousVehicle has two play methods. What to do?

Interfaces are a very powerful concept
• Separates implementation from specification at a fine granularity
– A class may implement many distinct interfaces

• Different from interfaces in languages like
Modula-2 and Modula-3 or header files
– Granularity
– Inheritance of interfaces


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







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

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


Widening conversions: subtypes
• Can convert from S to T if,
– if S is a subclass of T
– if S implements interface T
– if S is NULL and T is any class type, interface type or array type
– if S is a subinterface of T
– if S is an interface and T is ‘Object’
– if S is an array and