Composite Data Types
A composite data type is a type that is made up of other types. For example, here is a Roster
class that holds an array of Student
objects:
public class Roster {
private Student[] students;
private int numStudents;
public Roster(int size) {
students = new Student[size];
numStudents = 0;
}
public void add(Student s) {
// stub
}
public void remove(Student s) {
// stub
}
// Assumption: students' emails are unique.
public Student find(String email) {
return null; // stub
}
}
What is a method stub?
A stub is a method that doesn't actually do anything other than declaring itself and the parameters it accepts (and returning something valid, so the compiler is happy). Stubs are commonly used as placeholders for implementation.
Roster
is a class, so it too is an abstraction and a data type. However, it is different from the Student
class; the latter is to model a student in our problem domain, the former represents a collection of students. Sometimes this distinction is interesting, so to highlight it, we call a class like Roster
a container class or an aggregate class.
Object-Oriented programming paradigm is a powerful tool for modeling because it allows you to create composite data types (as well as a hierarchy of data types but more on these later).