• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Having confusion with variable override and method override

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having doubt over the difference between method and instance variable overriding when using polymorphism?
Like
class Test
{
public static void main(String[] args)
{
Base b=new subclass();
System.out.println(b.x);
System.out.println(b.method());

class Base
{
int x=2;
int method()
{
return x;
}
}
class subclass extends Base
{
int x=3;
int method()
{
return x;
}
}
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am having doubt over the difference between method and instance variable overriding when using polymorphism?



Well, basically the difference is... Polymorphism applies for instance methods. It does *not* apply for instance variables. Hence, there is no such a thing as overriding an instance variable.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic