Records: Records vs Classes
Comparison of Classes and Records
- We can compare classes and records in a number of ways:
- Types
- Template for Memory Allocation
- Encapsulation of data and routines
- Access Control
- Underlying Semantics
- User-defined, Structured Types
- Composite Objects
- Memory Allocation
- Partial Summary:
-
a class provides several features: a (structured, user
defined) type, a template,
encapsulating methods, and providing access control.
-
In Ada these
features are split: records provide the type and template, and packages
provide encapsulation and access control.
Comparison of Classes and Records - Types
- Classes and records both provide a means of declaring a type that has
fields and that can be used for declaring variables
Comparison of Classes and Records - Memory Allocation Template
- A class and a record both provide a template for allocation of memory:
- More details on memory allocation:
- A class declaration does not allocate
memory for the instance fields
- allocation occurs when an object of the class is created.
-
a record declaration does not allocate memory for its
fields
- allocation does not occur until a variable of the record type is
declared
- For completeness, let's mention class variables (ie those
declared with static):
Allocation of class variables is different from allocation
of instance variables:
class variables are allocated when the class is first referenced.
Later we will see the ada equivalent, which is related to
packages, not to records.
Comparison of Classes and Records - Encapsulation
- Two separate concepts:
- Declaration of structured type
- Encapsulation of methods/routines
- Java: A class usually contains (ie encapsulates) methods,
- some of the methods operate on the implied parameter
this
which is of the type of the class
- Ada: The record does not contain anything other than one or more fields.
- The declaration of the record type is frequently followed by
declaration of routines that operate on an explicit parameter
of the record type.
- Later we will see that a package can be used
to encapsulate type and record definitions to make them available
for clients, which is a service done by a class.
- As a final point of comparison regarding methods,
some classes, such as class Math,
are used as libraries of static methods that are normally
accessed without an object. In Ada, libraries are provided by
packages.
Comparison of Classes and Records - Access Control
- In Java, access control can be specified for each feature in a class
- In Ada, access control is provided in packages. It is not
provided inside records
Comparison of Classes and Records - Underlying Semantics
- Java uses reference semantics: variables are references to
instances of a class
- Ada uses value semantics: variables that are instances of a record
are values, not references
- Reference semantics are used for record parameters and
when pointers are explicitly used
- Even though the languages have similar syntax,
the underlying allocation and operations are different
Comparison of Classes and Records - User Defined Types
- User defined, structured types can be primitive
(ie have value semantics) in Ada, but MUST be reference types in Java
- Reference semantics are used for record parameters and
when pointers are explicitly used
Comparison of Classes and Records - Composite Objects
- Ada: composition of types can be primitive (ie
nested fields) or reference - done with pointers (which we'll see later)
- Java: composition is always implemented with reference
types (ie pointer fields), since there is no other way
Comparison of Classes and Records - Memory Allocation
- Memory allocation diagram: what's really allocated in memory?
- We've seen plenty of memory diagrams: Java has lots of pointers, Ada none (unless we
declare them explicitly)