0
can any one please give a very simple code to understand composition and aggregation in language java , Thank you.
language sholud be java
5 Respostas
+ 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.
+ 4
Okay, good to hear. It would be best if you could show your code attempt, so the community can assist you. 😊👍
+ 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 ......
+ 1
ok Sir Anthony Quick
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() );
}
}