Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt Regarding Overriding Method,Variables

 
Varsha Joshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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
 
Kaydell Leavitt
Ranch Hand
Posts: 690
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know.

That's a good question.
 
Subhankar Kushary
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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.
 
Uttara Rishi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • 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.
 
Douglas Boff Nandi
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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'.
 
Milton Ochoa
Ranch Hand
Posts: 336
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excelent Question and Reply.

Thank you everyone!

 
Varsha Joshi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Everyone for reply....

now i am through this concept

Thanks,
Varsha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic