Packages: Comparison with Classes and Summary
Package and Record and Classes
Packages and Records and Classes
- Records and Packages combined are similar (but not
identical) to classes:
- Packages contain procedures, classes contain
methods (ie encapsulatation)
- Packages contain Types, classes are types
- Fields in record types correspond to
fields in Java classes
- But, fields in objects are always ... (what?)
- Packages can contain multiple types; classes are a
single type
- Although, a class can contain nested classes
- Packages and classes both allow access control
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
- Example: Compare these two
Pair
clients:
// Java
class Pair{
int x, y;
String toString(){
return "(" + x + ", " + this.y + ")";
}
int distanceBetween(Pair p){ // Manhattan distance
return Math.abs(p.x - this.x) + Math.abs(p.y - this.y);
}
}
// Client
Pair p1 = new Pair(11, 12);
Pair p2 = new Pair(21, 22);
S.o.p(p1.toString());
S.o.p(p2.toString());
S.o.p(p1.distanceBetween(p2));
-- Ada
type Pair is record x, y: integer; end record;
function toString(p: Pair) return String is begin
return "(" & p.x'img & ", " & p.y'img & ")";
end toString;
function distanceBetween(p, q: Pair) return String is
(abs (p.x - q.x) + abs (p.y - q.y)); -- Expression function
Pair p3 = (41, 42);
Pair p4 = (41, 42);
begin
put(toString(p3));
put(toString(p4));
put(distanceBetween(p3, p4)
In both clients, one or two Pairs is passed as parameters to toString and distanceBetween
In the java code, p1 and p2 are implicit parameters to toString and
in the call to distanceBetween, p1 is implicit and p2 is explicit.
In the Ada code, p3 and p4 are always explicit parameters
Consider a diagram showing the variables and the parameters, in both languages
- Notice how we typically don't distinguish the variable's type
and the object's class
Implicit and Explicit Parameters, Continued
- In Ada, the explicit formal parameters are
p
and q
- First,
p
takes the value of p3
, and
then p takes the value of p4
- The explicit formal parameter must be used to access fields (ie access with p.x)
- Then, in the call to distanceBetween, p and q, respectively,
take the values of
p3
and p4
.
- In Java, the implicit formal parameter is
this
- First, this takes the value of
p1
, and then this takes the value of
p2
- In distanceBetween,
p1
is this, and
p2
is p
- The implicit formal parameter is optional in accessing fields
(ie access with
x
or this.x
)
Packages, Classes, Modules, Types
Conflating Modules and Types
- Java conflates the mechanisms of type and module
- To conflate is to join together different things
- Ada separates the concepts of a type module and a module
- 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 values and a set of operations
- Example: integers - values=..., operations=...
- Example: Strings - values=..., operations=...
- 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)
- Also see list in lecture notes
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
- procedures and functions
- constants
- exceptions
Summary
Summary: Ada Package Specification
- Has two sections: public and private
- Public section is everything above keyword
private
- Private section is everything following keyword
private
- Public section can define following services:
- Names of (private) Types
- Can also provide type implementation, but normally we follow good software
engineering practice and keep the implementation private
- Procedure and function signatures
- Constants
- Exceptions
- Private section typically contains
- Implementation of private types
- Private constants and types
- Example: Constant MaxWordSize in WordPkg
- Example: Constant size and type BigNum in BigNumPkg
Summary: Ada Package Body
- Must contain an implementation of every procedure and routine defined in
specification
- Signatures in package and body must match exactly (eg name, mode, type of parameters)
- Private section of package specification is visible in package body
- Package body is not visible to outside
- Package body can contain routines, types, constants, etc that are not defined in
the specification. These routines are private to the body.
Summary: Ada Package Client
- The client must have a use clause for any package it refers to
- The client can only see the public section of the spec
Summary of Visibility of Packages
- Specification contains public and private sections
- Client and body can access items declared in the public
section of the specification
- Body can also access items declared in the private section of
the specification
- Entire body is completely private.
- Entities declared in the public section can have a
private implementation,
which is given in the private section
- Why does the spec contain the implementation?
Packages: Summary Overview
- Similar to classes: A class is a type, a package contains a type
- Packages contain code that provides some or all of the following
services for clients:
- New Types
- Procedures and functions that operate on those types
- Constants, either of new or existing types (eg Null: Word, or Pi: Float)
- Procedure and function libraries (Not necessarily using new types)
- Exceptions
- Implementation uses two files
- mypkg.ads: Specification for Package MyPkg
- mypkg.adb: Body (ie implementation) for Package MyPkg
- Specification is similar, in some ways, to a java interface