• Post Reply Bookmark Topic Watch Topic
  • New Topic

RTTI - what does this mean  RSS feed

 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The intent of Java is that you use polymorphic method calls throughout your code, and use RTTI only when you must.

Thinking in Java, Third Edition. (Pg. 476)


What does this mean. Don't polymorphmic calls depend on RTTI? How can you make polymorphmic calls at runtime w/o knowing the exact type.

regards,
vijay.
 
Justin Yao
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Vijay Raj
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

regards,
vijay.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RTTI = Run Time Type Information
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!