aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Having confusion with variable override and method override Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Having confusion with variable override and method override" Watch "Having confusion with variable override and method override" New topic
Author

Having confusion with variable override and method override

Rajesh k Jha
Ranch Hand

Joined: May 10, 2009
Posts: 72
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
Sheriff

Joined: Sep 28, 2004
Posts: 19073
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having confusion with variable override and method override