• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting of this keyword in java

 
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can we cast the this keyword. for example consider this code in which I want to access the send() of class A. But not by creating the object of A, but by using super or this.


I have done this problem like this, it compiles fine but create a runtime error, a strange error like infinite look. Can someone explain why is it so and please also tell me how can I solve this problem(means is there any way to call send() of A without using object of A.
 
Ranch Hand
Posts: 472
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
{ ((A)this).send();
you have object of C -> so because of overriding you invoke send() of C.
I modify you code little:
 
Ranch Hand
Posts: 46
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Puspender

In send() of C, by using super, you can access the overridden method of B (but not send() of A).
C doesn't directly override send() in A and an instance of A would be needed to use the method.

Your code throws a StackOverflowError due to looped calling of send()

The reference to A produced by (A)this is valid but actually has no effect here
because at runtime the send() of C is invoked and so it calls itself forever.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you deniel and sergej

Daniel Clinton wrote:The reference to A produced by (A)this is valid but actually has no effect here
because at runtime the send() of C is invoked and so it calls itself forever.


but just look at my modified code.

here I have not use send(), and I am forcing send() of A, but don't know why send() of B is invoking??
 
Daniel Clinton
Ranch Hand
Posts: 46
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Instance methods bind at runtime according to the actual class of the object.
Your object is of class C and so it is the send() method available in class C that is called.
In your modified code C's send() is inherited from B.

Also, if you remove the send() method of B, B will inherit from A, and C from B and ' AA' will be output.
 
Puspender Tanwar
Ranch Hand
Posts: 658
2
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Daniel Clinton wrote:
Also, if you remove the send() method of B, B will inherit from A, and C from B and ' AA' will be output.


so overall the conclusion is that we don't have any way to directly access the A send() from C ???
 
reply
    Bookmark Topic Watch Topic
  • New Topic