Let's learn java programming language with easy steps

Monday, 13 March 2017

Aggregation in Java

 

Relationship in java

In java programming language, there are 3 types of relationship 
  • IS-A relationship(we discussed)
  • Has-A relationship(Aggregation)
  • Uses-A relationship
these above relationship tell us how to reuse the features or properties of one class to another class.


Is-A Relationship

Is-a relationship represents parent-child relationship in inheritance. In Is-a relationship one class extends another class to reuse the properties and function of that class or parent class.

In Is-a relationship, there exists logical memory space.
Is-a relationship in inheritance  in java

In the above diagram class B extends class A.

For example:

class Faculty
{
float salary = 25000;
}
class Science extends Faculty
{
float bonus = 4000;
public static void main(String args[])
{
Science s = new Science();
System.out.println("salary is : "+s.salary);
System.out.println("bonus is : "+s.bonus);
}
}

output: salary is : 25000.0
             bonus is : 4000.0


Aggregation in Java

Aggregation in java means, if a class have an entity reference it is known as aggregation. Aggregation represents Has-a relationship in java.

Aggregation is a special form of association and it is one way association.

consider a situation there is  student object contains many informations such as id, name , email-id etc. It contains one more objects named address, which contains its own information such as city, state, country, zipcode etc. Let's see in below example:

class Student
{
int id;
String name;
Address address;//Address is a class
-----
-----
}

In the above class here Student class have an entity reference address, so relationship is Student Has-a address.


Why use  Aggregation?

  • To maintain code re-usability.



Example of Aggregation

In this example, Student has an object of Address , address object contains it own information such as city, state, country etc. So there is Student Has-a address relationship.

For example:

//Address.java

public class Address
{
String city, state, country;
public Address(String city, String state, String country)
{
this.city = city;
this.state = state;
this.country = country;
}
}

//Student.java

public class Student
{
int id;
String name;
Address address;

public Student(int id, String name, Address address)
{
this.id = id;
this.name = name;
this.address = address;
}

void information()
{
System.out.println(id+" "+name);
System.out.println(address.city+""+address.state+""+address.country);
}

public static void main(String args[])
{
Address ad = new Address("New Delhi", "Delhi" , "India");
Address ad1 = new Address("Lucknow", "UP" , " India");

Student s = new Student(100, "Ranbir", ad);
Student s1 = new Student(101, "Varun", ad1);

s.information();
s1.information();
}
}
output: 100 Ranbir
             New Delhi Delhi India
             101 Varun
             Lucknow UP India


Uses-A Relationship

In Uses-a relationship, A method of one class is using an object of another class.

For example: 

class Employee 
{
float salary = 30000;
}
class Salary extends Employee
{
void show()
{
float bonus = 5000;
Employee e = new Employee();
float total = e.salary+bonus;
System.out.println("total salary is : "+total);
}
}

class Developer
{
Salary s = new Salary();
s.show();
}
}

output: total salary is : 35000.0

As long as the method is execution the object space is exists and once the method execution is completed automatically object memory space will be destroyed.

Q. Where to use inheritance and aggregation.

Ans. Inheritance is useful when you need to use and modify data member(property) and member function(behavior) of a class inside your class. Aggregation is better option When you need to use data member(property) and member function(behavior) of a class without modifying it inside your class.
Share:

0 comments:

Post a Comment

Facebook Page Likes

Follow javatutorial95 on Google+

Follow javatutorial95 on twitter

Popular Posts

Get Updates By Email

Link

Translate