Nic, You are absolutely right.dynamic binding, polymorphism, and OO-Design require inheritance. if those objects are not related thre is no point to mix them. Remenber the goal of overloading methods to define behaviour.
Mario, On Sat, Jan 2, 2010 at 7:56 AM, Nic Fox <[email protected]> wrote: > Hi > I've been doing a bit more reading, and it has confirmed something that I > was thinking of mentioning in my earlier reply. Basically I was going to > write that it's a matter of programming style as to whether or not you mix > object types in a Collection such as an ArrayList. One of the things I just > read states that it is usually considered bad style to mix object types in > Collections. > > As a guide I would look at the relationship between the objects, so for > example mixing child objects of a shared parent class would be OK, mixing > objects implementing an interface they have in common would be OK, but > mixing unrelated objects with nothing much in common doesn't make much > sense. > > Also the behaviour I described where the ArrayList automatically converts > primitive types to their equivalent wrapper class objects is a new feature > of Java called autoboxing. It will only work in Java 5 onwards. > > Nic > > Begin forwarded message: > > *From: *Nic Fox <[email protected]> > *Date: *2 January 2010 3:14:20 PM > *To: *[email protected] > *Cc: *MARIO CAMPAZ <[email protected]>, "[email protected]" < > [email protected]>, Java Programming Online Training Course By Sang Shin > <[email protected]> > *Subject: **Re: [java programming] problem with objects in arraylist* > > The original post was about an ArrayList which is different to an array. > One aspect is that ArrayList can only hold objects. Because of that, if your > code absolutely has to identify the type of object you can always use the > instanceof keyword in an if statement. Or as Mike Conley and Mario Campaz > pointed out, you can use features of polymorphism to do whatever is > appropriate to each object instance automatically. > > BTW, if you put a primitive type into an ArrayList it will be converted > automatically to the equivalent wrapper class object so for example int > becomes Integer, boolean becomes Boolean etc. You can also put a null > reference into an ArrayList but that's not recommended because you can end > up with null pointer exceptions if you try to do something with that > element. > > On 01/01/2010, at 2:12 PM, Retnuh wrote: > > Well there are ways of pulling two different objects out from an Array, > > but you will pull them all out and how are you sure of which one you are > > going to get? > > > Stephen > > > > > > > > MARIO CAMPAZ wrote: > > > To answer this: > > > Why did you put them in one array instead of seperating them into > > different ones. I thunk you should have seperated them at first and > > add them to the corrct array. > > > If you put them in one array, then you can exploit the polymorphic OO- > > design capabilities. > > Leaving the runtime engine take care of the differences using dynamic > > binding. > > Please, correct if I am wrong! > > > Mario > > > > On Thu, Dec 31, 2009 at 1:45 AM, Stephen Hunter <[email protected] > > <mailto:[email protected] <[email protected]>>> wrote: > > > Why did you put them in one array instead of seperating them into > > different ones. I thunk you should have seperated them at first and > > add them to the corrct array. > > > Now how are you tellingthe difference between the coffee and juice? > > This will probably be the equation to check against in your if > > statement. > > > Now for the "for" loop, there is an array called a "for each" array > > witch will itterate thru an array of objects. Just Google it and you > > will see the syntax, it is easy to use. > > > > Stephen > > > > > > > > > Sent from my iPhone > > > On Dec 30, 2009, at 6:36 PM, DrybLrac <[email protected] > > <mailto:[email protected] <[email protected]>>> wrote: > > > hello all I have an arraylist problem...I created a program to store > > coffee and juice objects in a arraylist bout how do I display them > > with a for loop. Inside the for loop I know I need a if > > statement to > > see if the current object in the for loop is coffee or juice. but > > don't know how to write it...any suggestions? > > > -- > > To post to this group, send email to > > [email protected] > > > <mailto:[email protected]<[email protected]> > > > > To unsubscribe from this group, send email to > > [email protected] > > > <mailto:javaprogrammingwithpassion%[email protected]<javaprogrammingwithpassion%[email protected]> > > > > For more options, visit this group at > > http://groups.google.com/group/javaprogrammingwithpassion?hl=en > > > -- > > To post to this group, send email to > > [email protected] > > > <mailto:[email protected]<[email protected]> > > > > To unsubscribe from this group, send email to > > [email protected] > > > <mailto:javaprogrammingwithpassion%[email protected]<javaprogrammingwithpassion%[email protected]> > > > > For more options, visit this group at > > http://groups.google.com/group/javaprogrammingwithpassion?hl=en > > > > > -- > > To post to this group, send email to > [email protected] > > To unsubscribe from this group, send email to > [email protected] > > For more options, visit this group at > http://groups.google.com/group/javaprogrammingwithpassion?hl=en > > > > -- > To post to this group, send email to > [email protected] > To unsubscribe from this group, send email to > [email protected]<javaprogrammingwithpassion%[email protected]> > For more options, visit this group at > http://groups.google.com/group/javaprogrammingwithpassion?hl=en > -- To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/javaprogrammingwithpassion?hl=en
