0

can any one please give a very simple code to understand composition and aggregation in language java , Thank you.

language sholud be java

9th Feb 2020, 8:20 AM
Rehan Usmani
Rehan Usmani - avatar
5 Respuestas
+ 5
Hi Rehan, is this a homework question? Please clarify what exactly you need help with, so that our lovely community members can attempt to assist you. Cheers.
10th Feb 2020, 2:59 PM
Tony
Tony - avatar
+ 4
Okay, good to hear. It would be best if you could show your code attempt, so the community can assist you. 😊👍
10th Feb 2020, 3:35 PM
Tony
Tony - avatar
+ 1
no Sir Anthony Quick , this is not a home work question ,i was trying to make my concepts strong in java , so when i go through composition ,i was facing problem that why are we refrencing object of other class in constructor ,, like their are two classes , first of job and other of person , so we are initializing object of job in person constructor . ,,, why is that so ,,,,, i hope i made my question clear , if not so please tell me ill post the whole code which i had done ......
10th Feb 2020, 3:34 PM
Rehan Usmani
Rehan Usmani - avatar
+ 1
10th Feb 2020, 3:36 PM
Rehan Usmani
Rehan Usmani - avatar
0
Sir Anthony Quick here's the code : class job { private int id; private String role; public void setId(int id_set) { id=id_set; } public int getId() { return id; } public void setRole(String role_set) { role=role_set; } public String getRole() { return role; } } class person { private job j; person() { j= new job(); j.setId(12); j.setRole("manager"); } public String getResult() { return j.getRole(); } public int GetID() { return j.getId(); } } public class Main { public static void main(String[] args) { person p = new person(); System.out.println(p.getResult() ) ; System.out.println(p.GetID() ); } }
10th Feb 2020, 3:48 PM
Rehan Usmani
Rehan Usmani - avatar