January 2015
Beginner to intermediate
392 pages
8h 44m
English
Polymorphism roughly means different forms. But what does it mean to us?
In the simplest words possible, any subclass can be used as a part of the code that uses the superclass.
For example, if we have an array of animals, we could put any object that is of a type that is a subclass of Animal in the Animal array, perhaps cats and dogs.
This means that we can write code that is simpler and easier to understand and modify:
//This code assumes we have an Animal class //And we have a Cat and Dog class that extends Animal Animal myAnimal = new Animal(); Dog myDog = new Dog(); Cat myCat = new Cat(); Animal [] myAnimals = new Animal[10]; myAnimals[0] = myAnimal;//As expected myAnimals[1] = myDog;//This is OK too myAnimals[2] = myCat;//And this ...
Read now
Unlock full access