There are two types of polymorphisms in Java. Compile time and Runtime
polymorphism.
so whatever the code snipets u have given are examples of runtime
polymorpism or u can say Upcasting.
Because methods(variables) from which of the classes (base or derived) will
be called is decided at the runtime only.
Upcasting takes advantage of polymorphism. An example of polymorphism:
- Code:
class A {
}
class B extends A {
}
B can be referred to as an object of type A or B.
Upcasting works like this:
B aBobj = new B ();
A anAobj = (A)aBobj;
Because aBobj can be referred to by the class names B and A, this code
will never fail. You might say that upcasting and type extension is the
implementation of polymorphism in Java.