Packages - Discussion
Packages vs Classes
Conflating Modules and Types
- Java conflates the mechanisms of type and module
- To conflate is to join together
- Ada separates the concepts of a module and a type
- For our purposes, a module is a syntactic unit that combines program units
such as types, constants, and subroutines
Comparison: Types, Classes and Packages
- A Java class defines a type
- A type is a set of ... and a set of ...
- To define the type, the class defines 2 things:
- The set of values for the type:
- Specified by the fields of the class/object
- The fields specify the structure of an instance of the class
- The operations for the type
- The methods that operate on an instance of the class
- Specified (obviously) by methods in the class
- Class also provides access control for hiding implementation
- Compare classes with packages:
- Package contains a type definition (typically a record type)
- Package contains definitions of procedures and functions
- That operate on that type.
- The package's private section also provides access control
Comparision of Classes and Packages: Classes
- What is a class?
- A Type for declaring reference variables
- A template for creation of instances of the class (ie
allocating instance variables)
- A repository of methods that operate on instances of the class
(with implicit parameter
this
)
- Methods with void return type (ie procedures)
- Methods with non-void return type(ie functions)
- A repository (ie a library)
that can be used without instances of the class, containing
- Constants (eg Math.PI)
- Methods with non void return type (eg Math.sqrt(...)
- Methods with void return type (eg System.gc(), System.exit()
- Other classes (eg class java.awt.geom.Ellipse2D.Double is
a nested public static class defined in the abstract class
java.awt.geom.Ellipse2D)
Comparision of Classes and Packages: Packages
- Packages provide similar services:
- They contain types (rather than are types
- They contain procedures and functions that operate on
instances of the types
- Explict parameters are required
- They are libraries of
- types
- procedures and functions
- constants
- exceptions
Implicit vs Explicit Parameters
Implicit (Java) vs Explicit (Ada) Parameters
- The difference between packages and classes highlights the
existence of Java's (and other typical oo languages') implicit parameters
- Consider the difference between these two pairs of statements:
// Java
Sop(p3.distanceToOrigin());
Sop(p3.distanceBetween(p4));
-- Ada
put(distanceToOrigin(p3));
put(distanceBetween(p3, p4));
In the java code, p3 is an implicit parameter. In the Ada code,
p3 is an explicit parameter
Consider a diagram showing p3, p4 and the parameters, in both languages
- Notice how we typically don't distinguish the variable's type
and the object's class
Another example: Compare these two Pair
clients:
// Java
Pair p5 = new Pair(1, 2);
Pair p6 = new Pair(3, 4);
S.o.p(p5.toString());
S.o.p(q6.toString());
-- Ada
declare
Pair p5 = newPair(1, 2);
Pair p6 = newPair(3, 4);
begin
put(toString(p5));
put(toString(p6));
In both clients, the Pair is passed to as a parameter to toString
In Ada, the explicit formal parameter is p
- First, p takes the value of p5, and then p takes the value of p6
- The explicit formal parameter must be used to access fields (ie access with p.x)
In Java, the implicit formal parameter is this
- First, this takes the value of p5, and then this takes the value of
p6
The implicit formal parameter is optional in accessing fields (ie access with x
or this.x)
Separate Dependency Hierarchy for Specification and Body
Specification and Body Dependency Hierarchy
- Consider this code fragment:
with pkg1;
procedure client is begin null;
end client;
with pkg2;
package pkg1 is null;
end p1;
with pkg3;
package body pkg1 is null;
end p1;
package pkg2 is null;
end p2;
package body pkg2 is null;
end p2;
package pkg3 is null;
end p3;
package body pkg3 is null;
end p3;
What is a package dependency graph for this program
What happens if we change various files?
What happens if our systems has tens of thousands of packages?
Context: Java Classes and Interfaces
- In Java, classes and interfaces accomplish (at least) 5 things:
- Type for reference variables:
- Template for creating objects:
- Example: p = new Person();
- Creates an object containing all fields for a Person
- Library for methods (ie encapsulates methods)
- Instance Methods [for objects] (eg y = p.getName())
- Static Methods [no objects] (eg z = Math.sqrt(x);
- Access control: eg private fields and methods
- Separation of specification and implementation
- Interfaces and private members
- Ada accomplishes the same things using types, declarations, and packages