If we take the code below:
Shape p1 = new Square();
Square c1;
if(p1 instanceof Square) {
c1 = (Square) p1;
}
What does it mean t
Polymorphism lets you change the behaviour of something depending on which type is it. Not sure how to explain it with your example since you could just assign it to a Square straight away if it is important that it is a Square for some reason. Sometimes you need to subclass as it might have additional behaviour etc, but consider this example:
class Shape
{
abstract void draw();
}
class Square extends Shape
{
void draw()
{
// square drawing goes here
}
}
The draw method here is an example of polymorphism as we have a base class Shape that says that all shapes now how to draw themselves, but only the Square knows how to draw a Square.