Search This Blog

Sunday, 9 March 2014

Aggregation in Java

/*
Aggregation means using the object of one class in another without inheriting.
Example:
class Student
{
  //Codes
}
class Address
{
 Student s;
 ...// Codes
}
*/



class Book
{
    private int bCode;
    private String bName;
    public void SetbCode(int n)
    {
      bCode=n;  
    }
    public void SetbName(String name)
    {
        bName=name;
    }
    public int GetbCode()
    {
        return bCode;
    }
    public String GetbName()
    {
        return bName;
    }
}
class Student
{
    int roll;
    String name;
    Book bk;
    Student(int roll,String name,Book k)
    {
        this.roll=roll;
        this.name=name;
        bk=k;
    }
    void showDetails()
    {
        System.out.println("Student name is : "+name+"  "+roll);
        System.out.println("Book details are : "+bk.GetbName()+"  "+bk.GetbCode());
    }
}


public class Aggregation
{
   public static void main(String[] args)
   {
       Book bt=new Book();
       bt.SetbCode(1007);
       bt.SetbName("MicroProcessor");
       Student s=new Student(21,"sanjay kumar singh",bt);
       s.showDetails();
   }
}

Output


No comments:

Popular Posts