This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I use reflection to invoke a method... I define a super class(StubImpl) object and by invoking a method of another class, which returns the sub class(CodesServicePortType_Stub) object instantiation for this super class object. And its required that I should cast the subclass object to instantiate the super class object. And this casting i want to be done dynamically. I know the name of the sub class(CodesServicePortType_Stub). What shud be done??
you'd like to have the type you are casting to more dynamic? There's a two part answer - you can't, and you don't need to. What type did you declare the variable "binding"? Just cast to that type. The cast and assignment will work fine with any subtype of StubImpl returned by the getCodesServicePort() method.
Now if you need to call methods on CodesServicePortType_Stub that are not defined on StubImpl, we should maybe revisit the design and see why you're doing that.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Casting doesn't actually create or change anything at runtime, it just performs a check. The compiler must know what methods your objects have at compile time. Trying to cast to some dynamically-known type won't help. Learn how Java does OOP.
Joined: Sep 20, 2004
Stan James, The cast and assignment will work fine with any subtype of StubImpl returned by the getCodesServicePort() method.
I understand this and it works fine.. The getCodesServicePort() method returns an object of CodesServicePortType_Stub and when I change the code as