wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Differance in overriding variable and method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Differance in overriding variable and method" Watch "Differance in overriding variable and method" New topic
Author

Differance in overriding variable and method

Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Please check this code:
==========================================
class Base{
int i = 5;
void methodA(){
System.out.println("methodA from Base");
}
}

class Sub extends Base{
int i = 10;
void methodA(){
System.out.println("methodA from Sub");
}

public static void main(String a[]){
Base oBase = new Sub();
System.out.println(oBase.i); //still its refering to Base i and printing 5
oBase.methodA(); //calling sub class method.
}
}
===========================================
Here variable is pointing to Base class and method is pointing to Sub class. Why??? Your help will be appreciated.

Thanks.


Javardhan:
Salil S Fadnavis
Greenhorn

Joined: Jan 03, 2006
Posts: 3
Hi,

When an instance method is invoked on an object using an reference, it is the class of the object denoted by the reference and not the type that determines which method to invoke.

AND

When a field/memeber of an object is accessed using a reference it is the type of the reference and not the class of the current object that determines which field is called.

(above explanation is from KM)

so....
oBase.i calls field in class Base and
oBase.methodA() calls method of class Sub
Ja vardhan
Ranch Hand

Joined: May 09, 2005
Posts: 169
Thank you for your reply Salil.

I understood first point, but second one is not clear.
u said:
-----------------------------------------------------------------
When a field/memeber of an object is accessed using a reference it is the type of the reference and not the class of the current object that determines which field is called.
------------------------------------------------------------------
In my code- type of the reference is Sub and the object created from the class Base. According to your above statement- the member should be pointed to Sub.i but its happening differant.
Explain bit more please.

Thanks.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
Ja Vardhan,
Only methods can be overriden. Variables are never overriden.


Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Jaroslav Sedlacek
Greenhorn

Joined: Dec 31, 2005
Posts: 5
To be more accurate:

Only non-static methods can be overriden.
Rohit Bhatia
Greenhorn

Joined: Dec 19, 2006
Posts: 2
No offenses but I think, the variables can be overridden.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
I agree with Jaroslav.

Overridding only applies to instance methods.

Consider this example.



Here the reference type determines which variable is accessed.

In overriding, the runtime type of the instance determines which method is called.
Prasad Shindikar
Ranch Hand

Joined: Feb 18, 2007
Posts: 114
what Salil has mentioned is correct.
You cannot over ride a member variable, only instance methods can be over ridden.
If at all member variables seem to be over ridden, it has got something to do with shadowing than with over riding.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Differance in overriding variable and method