This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Differance in overriding variable and method

 
Ja vardhan
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Salil S Fadnavis
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ja Vardhan,
Only methods can be overriden. Variables are never overriden.
 
Jaroslav Sedlacek
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be more accurate:

Only non-static methods can be overriden.
 
Rohit Bhatia
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No offenses but I think, the variables can be overridden.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic