• 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

Variable Shadowing

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Hi,
Please help me out to understand these lines
When a method is invoked on an object using a reference,it is the class of the current object denoted by the reference,not the type of reference,that determines which method implement will be executed



what I got from this is:
The method from corresponding class will be called.


When a variable is accessed using a reference,it is the type of reference,not the type of class of current object denoted by reference,that determines which variable will be accessed.



What I got from this is:
The variables accessed via references results in value the current reference denotes in its corresponding class.



Output:
Large Bill:500.0
Large Bill:500.0
Small Bill:50.0
Large Bill
Small Bill
Small Bill
NO Bill



What actully "type of the reference" means



What I got is: The class Type the reference belong to.
If i am understanding wrong plz rectify me.

:roll:
 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Say B extends A, i.e. B is a subclass of A.

1)You could say:

A myATypeReferenceToMyATypeObject = new A();

The would give you a new object of type A and it would be assigned to a reference (myATypeReferenceToMyATypeObject myA) of type A.

2) You could also say:

B myBTypeReferenceToMyBTypeObject = new B();

The would give you a new object of type B and it would be assigned to a reference (myBTypeReferenceToMyBTypeObject myB) of type B.

3) You could also say

A myATypeReferenceToMyBTypeObject = new B();

The would give you a new object of type B and it would be assigned to a reference (myBTypeReferenceToMyBTypeObject ) of type A.


So...At runtime when you invoke a method on a reference, the method as to exist on that type.

E.g. if class B has method x, but class A does not, then:

This is ok:

myBTypeReferenceToMyBTypeObject.x();

But this would fail (at compile time actually):

myATypeReferenceToMyBTypeObject.x();

Howeber, if A also had method y and B is effectively overriding the same method y, then at runtime, even if you invoke the y method on an A type reference to a B type object, the y method overridden by B will be called.

Cheers,

Simon.
 
deshdeep divakar
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanx Simon I got it

 
I didn't like the taste of tongue and it didn't like the taste of me. I will now try this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic