Science, Tech, Math › Computer Science Aggregation in Java: Definition and Examples Aggregation implies ownership, not just association Print Florian Kopp/Getty Images Computer Science Java Programming PHP Programming Perl Python Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Paul Leahy Paul Leahy Computer Science Expert M.A., Advanced Information Systems, University of Glasgow Paul Leahy is a computer programmer with over a decade of experience working in the IT industry, as both an in-house and vendor-based developer. Learn about our Editorial Process Updated on January 29, 2019 Aggregation in Java is a relationship between two classes that is best described as a "has-a" and "whole/part" relationship. It is a more specialized version of the association relationship. The aggregate class contains a reference to another class and is said to have ownership of that class. Each class referenced is considered to be part-of the aggregate class. Ownership occurs because there can be no cyclic references in an aggregation relationship. If Class A contains a reference to Class B and Class B contains a reference to Class A then no clear ownership can be determined and the relationship is simply one of association. Read More How to Implement Association in Java By Paul Leahy For example, if you imagine that a Student class that stores information about individual students at a school. Now assume a Subject class that holds the details about a particular subject (e.g., history, geography). If the Student class is defined to contain a Subject object then it can be said that the Student object has-a Subject object. The Subject object also makes up part-of the Student object — after all, there is no student without a subject to study. The Student object, therefore, owns the Subject object. Examples Define an aggregation relationship between Student class and the Subject class as follows: public class Subject {private String name;public void setName(String name) {this.name = name;}public String getName(){return name;}}public class Student {private Subject[] studyAreas = new Subject[10];//the rest of the Student class} Cite this Article Format mla apa chicago Your Citation Leahy, Paul. "Aggregation in Java: Definition and Examples." ThoughtCo, Aug. 26, 2020, thoughtco.com/aggregation-2033995. Leahy, Paul. (2020, August 26). Aggregation in Java: Definition and Examples. Retrieved from https://www.thoughtco.com/aggregation-2033995 Leahy, Paul. "Aggregation in Java: Definition and Examples." ThoughtCo. https://www.thoughtco.com/aggregation-2033995 (accessed March 29, 2024). copy citation