0

I'm trying to understand the difference between aggregation and composition in my code example. Below you can see my code example. We can see an abstract class of Person and a subclass of Student.

Since Person is abstract and can never create an object of itself, and Student is in need of Person to create its object. Do they have an composition as a relationship then? And is the code example an example of that?

public abstract class Person {
    private String name;
    private String email;
    private int phoneNmbr;

    public Person(String name,String email,int phoneNmbr){
        this.name = name;
        this.email = email;
        this.phoneNmbr = phoneNmbr;
    }
    public String getName(){
        return name;
    }
    public String getEmail(){
        return email;
    }
    public int getPhoneNmbr(){
        return phoneNmbr;
    }
}

public class Student extends Person {
    private String address;

    public Student(String name,String email,int phoneNmbr,String address){
        super(name,email,phoneNmbr);
        this.address = address;
    }

    public String getAddress(){
        return address;
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
  • *"Student is in need of Person to create its object"* where in the code is that supposed to be? – UnholySheep Sep 30 '17 at 11:15
  • @UnholySheep Primarily: `extends Person`, secondarily: `super(name,email,phoneNmbr)`. – Tom Sep 30 '17 at 11:23
  • 1
    It's neither Aggregation nor Composition, it is Inheritence. For Aggregation or Composition see https://stackoverflow.com/questions/11881552/implementation-difference-between-aggregation-and-composition-in-javan – Turo Sep 30 '17 at 11:23

0 Answers0