aspose file tools*
The moose likes Beginning Java and the fly likes Does Java support dynamic casting or only static casting? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Does Java support dynamic casting or only static casting? " Watch "Does Java support dynamic casting or only static casting? " New topic
Author

Does Java support dynamic casting or only static casting?

Himanshu V Singh
Greenhorn

Joined: May 06, 2011
Posts: 17



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

Joined: May 03, 2008
Posts: 4544
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
Himanshu V Singh
Greenhorn

Joined: May 06, 2011
Posts: 17
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

Joined: Apr 06, 2010
Posts: 4392
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18845
    
  40

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

Joined: May 06, 2011
Posts: 17
Thank you very much sir, very useful to me
Himanshu V Singh
Greenhorn

Joined: May 06, 2011
Posts: 17
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

Joined: May 03, 2008
Posts: 4544
    
    5

Himanshu, please don't surround your own responses with quote tags. It makes the thread difficult to follow.
Himanshu V Singh
Greenhorn

Joined: May 06, 2011
Posts: 17
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Does Java support dynamic casting or only static casting?