aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt Regarding Overriding Method,Variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt Regarding Overriding Method,Variables" Watch "Doubt Regarding Overriding Method,Variables" New topic
Author

Doubt Regarding Overriding Method,Variables

Varsha Joshi
Greenhorn

Joined: Jan 06, 2008
Posts: 10
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

Joined: Nov 18, 2006
Posts: 689

I don't know.

That's a good question.
Subhankar Kushary
Greenhorn

Joined: Feb 24, 2008
Posts: 4
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

Joined: Feb 24, 2008
Posts: 49
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

Joined: Apr 22, 2006
Posts: 424
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

Joined: Feb 25, 2008
Posts: 34
I am add getX() method. Look if I am correct.




Thank you;
Douglas Boff Nandi
Ranch Hand

Joined: Feb 25, 2008
Posts: 34
And I have another question, I can get A.x by getX method?
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
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

Joined: Oct 23, 2007
Posts: 336

Excelent Question and Reply.

Thank you everyone!

Varsha Joshi
Greenhorn

Joined: Jan 06, 2008
Posts: 10
Thanks Everyone for reply....

now i am through this concept

Thanks,
Varsha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt Regarding Overriding Method,Variables