Hello, I don't think Polymorphism calls depends RTTI. Polymorphism and RTTI are different concepts. What is polymorphism? In object-oriented programming, polymorphism is the ability to process objects differently denpending on their data type or class. For example, given a base class shape, polymorphism enables the programmer to difine different area methods for any number or derived classes, such as circles, rectangles , etc. No matter what shape an object is, applying the area method to it will return the correct results. In java, we get an object via reference. For example, Shape shape = new Circle(); shape get the reference to "new Circle", so the will know the exact type at runtime. But I am not sure, so please let me know when you get the answer
At runtime, the exact type is known and we should thank RTTI for that. What I want to ask is, why are these two treated as separate concepts. Only because of RTTI is the use of polymorphic methods justified.
No, not so much. What the passage means is that you should always prefer polymorphism to doing something like
There are all sorts of problems with this; the biggest one is that while polymorphism automatically handles the situation if a class C is added, this code would need to be changed. Another is that this code is much less efficient than using polymorphism.
So you see that in fact explicitly using RTTI is in some ways the exact opposite of polymorphism; it has none of the benefits, and many disadvantages.