Aggregation in Java
Aggregation in Java
package aggr;
class Address
{
int streetNum;
String city;
String state;
String country;
Address(int streetNum, String city, String state,
String country)//constructor in Address class
{
this.streetNum=streetNum;
this.city =city;
this.state = state;
this.country = country;
}
}
class Student
{
int rollNum;
String studentName;
Address studentAddr;
//Creating HAS-A relationship with Address
class//student has a address
Student(int rollNum, String studentname,Address
studentAddr)
{ // constructor of StudentClass
this.rollNum=rollNum;
this.studentName=studentname;
this.studentAddr = studentAddr;
}
public static void main(String args[]){
Address ad = new Address(55, "Agra", "UP",
"India");
Student obj = new Student(123, "Raina",ad);
System.out.println(obj.rollNum);
System.out.println(obj.studentName);
System.out.println(obj.studentAddr.streetNum);
System.out.println(obj.studentAddr.city);
System.out.println(obj.studentAddr.state);
System.out.println(obj.studentAddr.country);
}
}