• 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

Doubt Regarding Overriding Method,Variables

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One of the from Mock Exam

class A
{
int x = 5;
}
class B extends A
{
int x = 6;
}
class CoVarTest
{
public A getObject()
{
System.out.println("A");
return new A();
}
public static void main(String[] args)
{
CoVarTest c1 =new Test();
System.out.println(c1.getObject().x);
}
}
class Test extends CoVarTest
{
public B getObject()
{
System.out.println("B");
return new B();
}
}

The output is:
B
5

My Doubt is the method c1.getObject() calls the method "public B getObject()", then why the value of x 5 instead of 6?
I know that overriding is not for variables they are choosen at compile time.

Please provide some thought on this......

Thanks in Advance.

Regards,
Varsha
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know.

That's a good question.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer to K&B (Page - 98)

Polymorphic method invocations apply only to instance methods. You can
always refer to an object with a more general reference variable type (a superclass
or interface), but at runtime, the ONLY things that are dynamically selected
based on the actual object (rather than the reference type) are instance methods.
Not static methods. Not variables. Only overridden instance methods are
dynamically invoked based on the real object's type.



In your example in the line System.out.println(c1.getObject().x); c1.getObject() referes to a object of Class A at compile time.At runtime the getObject() for Class B is called polymorphically.But again for the instance variable x,class A version is printed.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody,

Shubhankar said....

In your example in the line System.out.println(c1.getObject().x); c1.getObject() referes to a object of Class A at compile time.At runtime the getObject() for Class B is called polymorphically.But again for the instance variable x,class A version is printed.
--------------------------------------------------------------------------------


I think the object returned is new B().
So the next invocation will be B.x.
Can somebody please explain?
Thanks in advance.


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

Originally posted by Uttara Rishi:
I think the object returned is new B().
So the next invocation will be B.x.


Yes, but the point here is that Variables access is determined at Compile time. So by the time this statement is executed c1.getObject().x this method call returns object of type 'A' (at Compile time) hence 'x' of 'A' is returned.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am add getX() method. Look if I am correct.




Thank you;
 
Douglas Boff Nandi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And I have another question, I can get A.x by getX method?
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't, even by casting ((A)c1.getObject()).getX() because of the polymorphic assignment at runtime the returned object is of sub type, so the overridden method will always be executed. To access 'x' through getX() I think the only way is to invoke the method through object of type 'A' or just don't override getX() in class 'B'.
 
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Excelent Question and Reply.

Thank you everyone!

 
Varsha Joshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Everyone for reply....

now i am through this concept

Thanks,
Varsha
 
Every plan is a little cooler if you have a blimp. And a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic