Polymorphism is a big fancy work that can be broken down into "poly" which means many and "morphism" which means form.
So, when you see "polymorphism", think "many forms".
In Java it means that the method that gets called at run-time (when the code is run) depends on the type of the object at run-time.
This is similar to a toy for toddlers that has pictures of animals.
When the handle is pulled an arrow spins; when it stops the toy plays the sound associated with the animal that arrow lands on:
INHERITANCE-BASED POLYMORPHISM
If you were simulating this toy with software you could create an Animal class that had a makeNoise method.
Each subclass of Animal would override the makeNoise method to make the correct noise for that type.
This type of polymorphism is called inheritance-based polymorphism.
You have a common parent class, but the behavior is specified in the child class.
Note: In Java and object variable has both a declared (compile-time) type and an actual (run-time) type.
The declared (compile-time) type of a variable is the type that is used in the declaration.
The actual (run-time) type is the class that actually creates the object using the new keyword.
For example, the nameList declared above has a declared type of List and an actual or run-time typeof ArrayList.
The compiler will check if the declared type has the methods or inherits the methods being used in the code and give an error if it doesn't find the method(s).
The List interface does have an add method so this code will compile with no errors.
At run-time the execution environment will first look for the add method in the ArrayList class since that is the actual or run-time type.
If it doesn't find it there it will look in the parent class and keep looking up the inheritance tree until it finds the method.
It may go up all the way to the Object class.
Again, if the method is not found the code will not compile.
The variable message declared above has a declared type of Object and an actual or run-time type of String.
Since the declared type of message is Object the code message.indexOf("h"); will cause an error because the Object class does not have an indexOf method.
At compile time, the compiler uses the declared type to check that the methods you are trying to use are available to an object of that type.
The code won't compile if the methods don't exist in that class or some parent class of that class.
At run-time, the actual method called depends on the actual type of the object.
Remember that an object keeps a reference to the class that created it (an object of the class called Class).
When a method is called at run-time the first place that is checked for that method is the class that created the object.
If the method is found there it will be executed.
If not, the parent of that class will be checked and so on until the method is found.
In the previous lesson about inheritance hierarchies we actually saw polymorphic behavior at run time in the following ways:
1) Polymorphic assignment statements such as Shape s = new Rectangle();
2) Polymorphic parameters such as print(Shape) being called with different subclass types.
3) Polymorphic array and ArrayList types such as Shape[] shapeArray = { new Rectangle(), new Square() };
In all of these cases, there are no errors at compile-time because the compiler checks that the "subclass is-a superclass" is true.
But at run-time, the Java interpreter will use the object's actual subclass type and call the subclass methods for any overriden methods.
This is why they are polymorphic - the same code can have different results depending on the object's actual type at run-time.
SUMMARY
At compile time, methods in or inherited by the declared type determine the correctness of a non-static method call.
At run-time, the method in the actual object type is executed for a non-static method call. This is called polymorphism.
EVIDENCE
1) Complete the following Google Form. This form must be 100% correct and includes released AP practice questions. To stop working and return later, hit submit! You can "edit your response" and continue where you left off.
2) Create Your Own Assignment! Remember, the OBJECTIVE of this lesson is to be able to call methods in an inheritance relationship. What EVIDENCE can you present to prove that you can do this? Meet with Mr. C when you are ready to present. In other words, show me what you can do!
3) MC Exam Practice: This lesson has additional practice problems which can be found on the MC Exam Practice page with an answer key! Use ctrl + f to search for different objectives throughout the year. You could look at these questions now, or, save these practice questions for later and use them to review! The choice is yours.