• 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

instance access

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//You can access class A's m1() using ( (A) this ).m1() from class C.----->false
i tried the above statement by coding for understatnding.but i am stuck that how to work this (A)t.m1() statement inside the C class.please help me.



Please help me with the way to see the result of (A)t.m1().
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Please help me with the way to see the result of (A)t.m1().



Well... you can't. The t references contains a three_dimensinal object. It behaves as a three_dimensinal object, even if you cast it to an A reference.

Henry
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you would need to write it a ((A)t).m1(), but that still won't have any effect on the method that's called.

Since m1() is defined in class Three_dimensinal, and t is a reference to a Three_dimensinal object, and Three_dimensinal overrides m1 from A and B, it doesn't matter if you call the instance a Three_dimensinal or B or A, the overridden method in Three_dimensinal will be called.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply Henry and Keith.


if you call the instance a Three_dimensinal or B or A, the overridden method in Three_dimensinal will be called.



I have a doubt in the above statement.

if i creat a B instance in Three_dimensinal class,then calling this.m1() or ((A)t).m1() gives mmmm result(not Three_dimensinal.m1() gets printed)

Then what is the meaning of quoted statement.Please help again.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

if i creat a B instance in Three_dimensinal class,then calling this.m1() or ((A)t).m1() gives mmmm result(not Three_dimensinal.m1() gets printed)



The key phrase in this quote is "if I create a B instance". A B instance will behave as a B instance, even if you cast it to an A reference. And casting it to a Three_dimensinal instance will cause an exception.

And just because a Three_dimensinal class has a reference to a B instance, doesn't make the B instance a Three_dimensinal instance.

Henry
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry- I have understood what you have told.My doubt is
please elaborate the following quote with coding.I am having trouble understanding the below.


if you call the instance a Three_dimensinal or B or A, the overridden method in Three_dimensinal will be called.

 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice in this code, the method call is going to decided by the runtime type of the object, not the reference type.



If the object is an object of type D, then whether you call it an A, a B, a C, or a D, when m1() is called on the object reference, the overridden method in D is called.
[ February 14, 2007: Message edited by: Keith Lynn ]
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for taking your time and typed everthing.

Ya keith i know very well about referencetype and object type and the overriding will look only RHS objecttype for methods.

The one thing that i got confused is in your statement you used instance in place of referenceType.


if you call the instance a Three_dimensinal or B or A, the overridden method in Three_dimensinal will be called.



My understanding is instance will mean only the object type.Am i correct or people call the referencetype too as instance( like the quote statement )
 
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic