+ 2

polymorphism. what is polymorphism?why we use it and when?

10th Jan 2017, 7:04 PM
shakil
shakil - avatar
2 odpowiedzi
10th May 2017, 12:54 AM
NimWing Yuan
NimWing Yuan - avatar
+ 8
Polymorphism is about having multiple forms.This is realized trough inheritance. So for example (as shown in teh java course) if you have teh class animal and then you have subclasses like cat, dog or penguin. They all are forms of animals and have things like legs, skincolour, and so on. So Polymorphism helps you when you have things like these. It is often used to make sure every subclass has specific methods/attributes from the superclass (as i mentioned above with the animals). So for example, if you make an animal game, every animal needs a possibility to move, but cats move different to penguin (4 legs vs 2 legs) code would look something like this: class animal{ void move(){ //move method } } class cat extends animal{ void move(){ moveFirstFeetFwd(); moveSecFeetFwd(); moveThirdFeetFwd(); moveFourthFeetFwd(); } } class penguin extends animal{ void move(){ moveFirstFeetFwd(); moveSecFeetFwd(); } } With the keyword "extends" you can let one class inherit from another Did you understand this or are there still questions you need answers to?
10th Jan 2017, 7:24 PM
ein siedler
ein siedler - avatar