File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The result of instance variable of inheritance.

 
Sam Sunamin
Ranch Hand
Posts: 113
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the result of the following code?

public class Question10 {

public A getObject() {
return new A();
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Question10 q10 = new SubQuestion10();
System.out.println(q10.getObject()); //line 10
System.out.println(q10.getObject().x); //line 11
}

}

class SubQuestion10 extends Question10{
public B getObject() {
return new B();
}
}

class A{
int x=5;
}

class B extends A{
int x=6;
}

When I run the code, I got the result as:
B@1372a1a
5



My question is why line 11's result is not 6, but 5.
But the result of line 10 is B@1372a1a, not A@1372a1a
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sam,

Try this modified code and understand what exactly is happening...

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic