Powered by Blogger.

Definition of Heterogeneous Collection and Program in Java

Definition of  Heterogeneous Collection:-

Heterogeneous collection is a collection of dissimilar objects or classes.

Heterogeneous Collection Program:-

 class Man{
   public void fly(){
     System.out.println(”Man can not fly”);
   }
 }

 class SuperMan extends Man{
   public void fly(){
     System.out.println(”Superman can fly”);
   }
 }

 class SpiderMan extends Man{
   public void fly(){
     System.out.println(”Spiderman can’t fly, but can jump”);
   }
 }

 public class Tes tHeterogeneous{
   public static void main(String[]args){
     Man[] arr = new Man[3];
     arr[0] = new Man();
     arr[1] = new SuperMan();
     arr[2] = new SpiderMan();
   }
 }

Example of a Heterogeneous Collection:-

I promised in Chapter 8, “Polymorphism and Abstraction,” that I would show you how to create a heterogeneous collection of Employee objects. The abstract Employee class has three child classes: Salary, Hourly, and Contractor. Suppose that we have a company that never has more than 200 employees. We could create three different arrays to keep track of the employees:

Salary [] salaries = new Salary[200];

Hourly [] hourlies = new Hourly[200];

Contractor [] contractors = new Contractor[200];


Using three different arrays has its disadvantages. First of all, using 200 references of each type is a waste of memory. But how do we distribute the employee types? What if one year we have 100 salaried, 100 hourly, and no contract employees, but a year later we have 150 salaried, 25 hourly, and 25 contract employees? I suppose we could start out with smaller arrays, but if any one of three arrays was too small, a larger array would have to be instantiated and the data from the smaller array copied into the larger array. The second disadvantage has to do with changes to our program in the future. What if a new type of Employee is added? Then, a new array is needed, and the question about how big it should be arises again.

We can avoid these types of situations by creating a single heterogeneous collection that contains all the employees of the company, no matter how the employee is paid. The Employee class is a common parent, so we can create an array of Employee references by polymorphism:

Employee [] company = new Employee[200];

The company array consists of 200 Employee references, and each reference can refer to either a Salary, Hourly, or Contractor object. If a new child class of Employee comes along in the future, these objects can appear in the company array as well. The MyCompany program uses the Employee class and instantiates 200 employees of random pay types, storing them in a single heterogeneous collection:

public class MyCompany{

public static void main(String [] args){

Employee [] company = new Employee[200];

System.out.println(“Randomly fill the array with employees”);

for(int i = 0; i < company.length; i++){

int random = (int) (Math.random() * 3);

if(random == 0){

company[i] = new Salary(“Salary “ + i, “New York, NY”, i, 50000.00);

}

else if(random == 1){

company[i] = new Hourly(“Hourly “ + i, “Chicago, IL”, i, 10.00);

((Hourly) company[i]).setHoursWorked(40);

}

else{

company[i] = new Contractor(“Contractor “ + i, “Denver, CO”, i, 200.00);

((Contractor) company[i]).setDaysWorked(5);

}

}

SmartBoss boss = new SmartBoss();

System.out.println(“Paying each employee”);

for(int i = 0; i < company.length; i++){

boss.payEmployee(company[i]);

  }

}

}

The payEmployee() method in SmartBoss has an Employee parameter, so all the elements in the company array can be passed in. Within payEmployee(), the computePay() and mailCheck() methods are invoked on the Employee passed in; and by virtual method invocation, the appropriate computePay() method is invoked on each of the 200 Employee objects. Because the MyCompany program generates random types of employees, the output will look different each time the program is executed.

4 comments:

  1. Excellent piece of knowledge, I had come back to read concerning your web site from my friend shiva, bangalore. I have readed atleast eight posts of your website and let me tell you, your website provides the most fascinating information. This is the knowledge that I had been craving for, I am already your rss reader currently and that I would frequently be careful for the new posts. Thanks plenty another time, Regards,Java Online Training

    ReplyDelete
  2. I was reading your blog this morning and noticed that you have a awesome resource page. I actually have a similar blog that might be helpful or useful to your audience.

    Regards
    sap sd and crm online training
    sap online tutorials
    sap sd tutorial
    sap sd training in ameerpet
    sap crm training tutorial

    ReplyDelete
  3. Pretty good post. I just came across your site and wanted to say that I’ve really enjoyed reading your posts. In any case I’ll be subscribing to your feed and I hope you will keep a good work!Cheer!


    sap online training
    software online training
    sap sd online training
    hadoop online training
    sap-crm-online-training

    ReplyDelete
  4. This is one awesome blog article. Much thanks again.
    I really enjoy the blog.Much thanks again. Really Great.


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete