• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays of Objects and interfaces  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studying for my Certs and keep seeing posts and things about the power of using arrays of different objects that implement the same interface. I haven't written or seen code that does this and I can't understand the power of this. Why is this one of the most powerful uses of interfaces? I get the feeling if someone would explain this to me, my understanting of interfaces and OOP in general would open up. Any help would be appreciated!
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christopher,

Welcome to JavaRanch!

Just pulling this out of my hat: imagine you're writing a payroll system. The first stage busies itself pulling information about employees, their wages, payscales, deductions, and indeed, status like full-time, part-time, contractor, manager... Every kind of employee is represented by a different class, which contains code to compute pay according to that employee's status. Furthermore, each kind of deduction has code associated with it, as does each kind of payment.

The second stage has to print the actual pay stubs. How should the first stage communicate with the second? Well, here's one way: it should construct an array of Employee objects. Say Employee is an interface, and many different classes implement it. Furthermore, say there is a Deduction interface, and a Payment interface. Now imaging that each Employee contains an array of applicable Payment objects, and an array of applicable Deduction objects, and the Employee interface provides access to these arrays or lists.

Now the second stage has it easy! It loops over the Employees, and asks each Employee to compute its pay. It loops over each Employee's Deductions, and asks each one to display itself. Same for the Payments.

There might be dozens or even hundreds of different kinds of deductions and payments and employees, but the paystub-printing routine is very simple. It lets each specially-coded Deduction worry about how to handle itself.
 
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christopher,
please take a few minutes to read this post.

I was talking about an example when you have several concrete cars (classes like Ferrari, Subaru, Toyota etc.) implementing the Car interface. If you had an array of cars like Car[] cars = new Car[]{new Ferrari(), new Subaru(), ...}; you could, for example iterate the whole array and start, stop, fill the tank with fuel at the same time without bothering checking if the car is a Ferrari or not, just because they all implement the same interface.
I think this is pretty cool and powerful! :-) Especially if you consider that the concrete cars might behave in a completely different way from each other, but still their being cars will keep something in common, that something are the functionalities assured by the interface.

I hope this is helpful!
Giovanni
 
Christopher Maze
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have an array of 100 Objects (Manager, Intern, Executive, ect.) each implementing the Payment and Deduction interfaces. I simply iterate over the array and execute the methods in the interface. I think I understand. Now couldn't I also just make an Employee class with abstract methods the are the same as the ones in the two interfaces and extend that class to Manager, Intern, ect. and do the same thing? Could you explain the benefit of doing it the previous way? Thanks.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my first Java class in college, I had an assignment where we needed to fill a Vector with several types of shapes then paint the shaps on a JFrame (in a GUI). I think the assignment required at least rectangles and circles. Perhaps there were other shapes as well, too, but I can't remember for sure. I think I used an abstract class called Shape (instead of an interface) and iterated through the elements of the Vector to paint them. The painting code never needed to know which kind of shape since the base class had an abstract method named paint() and the children classes all overrode this method to do the specific work for that particular type of shape.

Interfaces behave similarly, as described in the Employee example. Of course, this leads to your question: what's the difference between using an abstract class and an interface. I don't think I'll take time to go into it here since this question is asked often on this message board. Instead, I will refer you to the Search Tool so you can find the previous dicussions on this topic.

Layne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!