• 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

Differance in overriding variable and method

 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ja Vardhan,
Only methods can be overriden. Variables are never overriden.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be more accurate:

Only non-static methods can be overriden.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No offenses but I think, the variables can be overridden.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic