This is espescially used when Animal is abstract. I mean, what is an animal anyways??
This way you can have
and you can do animal things to all of them. And for the animal things that never change (like breathe(), eat(), or sleep()), that gets taken care of in the abstract superclass so they can all be taken care of without writing more code and no worries about someone doing this:
I dunno what would come of this in the real world.... so it would be best not to let it happen in a zoo program
When you do things right, people won't be sure you've done anything at all.
Janeice DelVecchio wrote:and you can do animal things to all of them.
Except the giraffe, they're hot.
Joined: Apr 02, 2010
thanks for your replies
but I still dont get it. if Dog extends Animal. If you write: Dog d = new Dog(); and Animal d = new Dog(); and then call a method d.eat(); the result will be the same.
I understand like polymorphic arguments. and i understand for example that if you had an animal array: Animal a = new Animal then you could say a = new Dog(); a = new Cat(); and then a.eat(); would call Dogs eat().
but if you only have an abstract Animal class which a Dog class extends. whats the point of doing Animal d = new Dog(); instead of Dog d = new Dog(); ?
basically i didnt understand :
Because that ties you to a specific List implementation, even if the implementation doesn't matter.
I did but my english is not the best so i was not so sure if i understood it. Is it if I want to change how an animal is? so then i would edit abstract Animal class , and the Dog class code that was inherited from Animal would change automaticly? if i wrote Animal a = new Dog(); ?
The idea is that the rest of the program does not need to know that 'things' is a LinkedList - the rest of the program just needs to know that it's a List, and what particular implementation of the List interface is behind it, is something that it shouldn't be concerned about.
The advantage of this is that you might later want to change it to a different implementation of List. Suppose that you discover that for this particular List, it would be more efficient to use an ArrayList. If you wrote the code as above, you have to change only that one line, to:
For the rest of the program, no changes are needed, because 'things' is still a List.
If you would have written:
the change might have been much harder, because now the rest of the program would have known that 'things' is a LinkedList, and maybe somewhere else in the program some method might have been called on 'things' that's specific to LinkedList.
This principle is called polymorphism and it's an important principle in object oriented programming.