In Java how instance of and type cast(i.e (ClassName)) works on proxy object ?

Posted by learner on Stack Overflow See other posts from Stack Overflow or by learner
Published on 2010-05-03T10:11:24Z Indexed on 2010/05/03 10:18 UTC
Read the original article Hit count: 202

Java generates a proxy class for a given interface and provides the instance of the proxy class. But when we type cast the proxy object to our specific Object, how java handles this internally? Is this treated as special scenario?

For example I have class 'OriginalClass' and it implements 'OriginalInterface', when I create proxy object by passing 'OriginalInterface' interface java created proxy class 'ProxyClass' using methods in the provided interface and provides object of this class(i.e ProxyClass). If my understanding is correct then can you please answer following queries

1) When I type cast object of ProxyClass to my class OriginalClass this works, but how java is allowing this? Same in case of instace of? 2) As my knowledge java creates a proxy class only with the methods, but what happen when I try to access attributes on this object? 3) Only interface methods are getting implemented in Proxy, but what happens when I try to access a method which not in interface and only mentioned in the class?

Thanks, Student

© Stack Overflow or respective owner

Related posts about proxy

Related posts about dynamic-proxy