If we take the code below:
Shape p1 = new Square();
Square c1;
if(p1 instanceof Square) {
c1 = (Square) p1;
}
What does it mean t
Some people believe that there is a time and a place for instanceof, and that the Visitor pattern isn't always a complete and appropriate replacement for it. Some other people hiss and scowl. Rinse, repeat.
I think your example could be improved by trying to do something meaningful (such as drawing, or counting sides, etc), because the OOP philosophy would fundamentally avoid the situation you illustrate in your example. For example, an OOP design would either declare c1 as a Shape rather than a Square, or simply use the p1 variable.
As an aside, if you're after the situation where c1 is null if it's not a Square, or set to p1 if it is, a similar "as" operator exists, which I'm a fan of.
Shape p1 = (Math.random()>0.5) ? new Square() : new Circle();
Square c1 = p1 as Square;
// c1 is null or not, depending on p1's type.
It's no more OO than instanceof in my view, but then again I don't really consider instanceof to be inately "non-OO".