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

Having confusion with variable override and method override

 
Rajesh k Jha
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • 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;
}
}
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic