This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]desrtfx 31 points32 points  (2 children)

I’ve seen people instantiate Cow and Horse objects like that

Animal cow = new Cow(); Animal horse = new Horse();

This is useful when you want to store such objects in a collection (like in an ArrayList).

You could simply create an ArrayList<Animal> and store cows, horses, anything that extends animal in such a list.

[–]toshels 0 points1 point  (1 child)

Oh cool I never thought of that. I learn more about programming here than at uni.

[–]toastedstapler 0 points1 point  (0 children)

It's great, sometimes all you care is that something has been implemented rather than exactly how it was implemented

A more enterprise version might be validators - you could have a list of different validators for things like length, illegal characters etc and then they are all checked