• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does Java support dynamic casting or only static casting?

 
Himanshu V Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



Hello i have marked the code snippet, please tell is there any other way of casting around in java like C++ dynamic casting, that static casting quite hard coded . Any way to avoid that ??
 
Darryl Burke
Bartender
Posts: 5131
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your previous thread,
jishnu dasgupta wrote:Please get rid of this idea of comparing c++ and java.Trust me they are way different although it might seem they have loads of similarities, and are OOP languages!!!


First off, you need to tell us your interpretation of static and dynamic in the context of casting, as this isn't standard terminology.
 
Himanshu V Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:In your previous thread,
jishnu dasgupta wrote:Please get rid of this idea of comparing c++ and java.Trust me they are way different although it might seem they have loads of similarities, and are OOP languages!!!


First off, you need to tell us your interpretation of static and dynamic in the context of casting, as this isn't standard terminology.


i meant in the code above i am trying to down cast (from super to sub-class) since its explicit casting, and i have resloved it using static casting by giving the name of the class from where i have to call the method.

i need to know that is there any other way around like dynamic casting so that i dont have to hard code the specific class name, code seems to be tightly coupled.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The type you're casting to has to be specified at compile-time, otherwise the compiler wouldn't be able to check that the methods you are calling exist.

I must admit, though, I'm a little confused as to what you're looking for. Because in my (limited, to be fair) experience of casting in C++, you still need to specify the type you're casting to in the code. That's seems to be the case with the dynamic_cast function, anyway. In fact, a Java cast (when using reference variables) is more like a C++ dynamic cast than a static cast, because of the way it does run-time checking.

If you want to reduce coupling in your code, you could introduce some interfaces. It probably wouldn't be worth it in the example you've given, but it demonstrates the principle:


[Edit: corrected mistake in code]
 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:
I must admit, though, I'm a little confused as to what you're looking for. Because in my (limited, to be fair) experience of static casting in C++, you still need to specify the type you're casting to in the code. That's seems to be the case with the dynamic_cast function, anyway. In fact, a Java cast (when using reference variables) is more like a C++ dynamic cast than a static cast, because of the way it does run-time checking.


Agreed. Java's casting is like C++'s dynamic_cast -- C++'s static_cast option, which is similar in behavior to C's casting, doesn't do any runtime type checking. This, of course, can break type safety, and isn't allowed in Java.

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21184
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu V Singh wrote:
i need to know that is there any other way around like dynamic casting so that i dont have to hard code the specific class name, code seems to be tightly coupled.



You can use the reflection libraries to do this. First, you need to load the class file using the Class.forName() method -- because obviously, you won't know the name of the class at compile time. Once you have the class loaded, you can cast an instance to it using the cast() method. IMHO, this method is just doing the type checking part, since it is technically not possible to declare a reference for a class that you don't know about at compile time.

And ANSI C++ doesn't have the equivalent of this -- that I know of.

Henry
 
Himanshu V Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much sir, very useful to me
 
Himanshu V Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:The type you're casting to has to be specified at compile-time, otherwise the compiler wouldn't be able to check that the methods you are calling exist.

I must admit, though, I'm a little confused as to what you're looking for. Because in my (limited, to be fair) experience of static casting in C++, you still need to specify the type you're casting to in the code. That's seems to be the case with the dynamic_cast function, anyway. In fact, a Java cast (when using reference variables) is more like a C++ dynamic cast than a static cast, because of the way it does run-time checking.

If you want to reduce coupling in your code, you could introduce some interfaces. It probably wouldn't be worth it in the example you've given, but it demonstrates the principle:



thanks for your time, just started with java, hope to learn more from you

 
Darryl Burke
Bartender
Posts: 5131
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Himanshu, please don't surround your own responses with quote tags. It makes the thread difficult to follow.
 
Himanshu V Singh
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Himanshu, please don't surround your own responses with quote tags. It makes the thread difficult to follow.


sorry i got it now, jst joined coderanch few dayz back
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic