PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

Java · Theory

Aggregation in Java

← All stacks

Theory

34/270

Aggregation in Java

Aggregation is HAS-A: one object uses another. Student has an Address. Model it with a field: Address address; inside Student. Not extends.

Address can still exist if Student goes away — weaker ownership. Inheritance is IS-A. Aggregation is HAS-A. Composition is a stronger “part-of” idea (engine inside car).

Using extends when the real relationship is HAS-A is the usual design mistake. Interview line: Student HAS-A Address — field reference, not extends.

Let's take this on the board with one tiny Main class — no extra files. Aggregation in Java — output: Asha - Pune. Address can exist without Student — that is aggregation (HAS-A), not IS-A. Start from main, go line by line, and stop at each print. That output is the proof for Aggregation.

Exam tip

Say HAS-A with Student/Address. Contrast with extends.

Example

class Address {
  String city;
  Address(String city) { this.city = city; }
}
class Student {
  String name;
  Address address; // HAS-A
  Student(String name, Address address) {
    this.name = name;
    this.address = address;
  }
}
public class Main {
  public static void main(String[] args) {
    Address a = new Address("Pune");
    Student s = new Student("Asha", a);
    System.out.println(s.name + " - " + s.address.city);
  }
}

Aggregation in Java — output: Asha - Pune. Address can exist without Student — that is aggregation (HAS-A), not IS-A.

Short notes

  • DefAggregation = HAS-A. One object uses another as a field.
  • RuleStudent has Address. Not Student extends Address.
  • DiffIS-A = inheritance. HAS-A = aggregation. Composition = stronger part-of.
  • RememberAddress can live even if Student is gone.

Questions

1

What is aggregation?

2

Student and Address — extends or field?

3

IS-A vs HAS-A?

Previous← Inheritance in JavaNextMethod Overloading in Java →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.