Hiden variable display from subclass.
class A{
int var;
A() {
var = 1;
}
public void display() {
System.out.println("var :: " var);
}
public static void main(String wrg[])
{
B obj = new B();
obj.display();
}
}
class extends B {
int var;
B() {
var = 2;
}
};
The output of the program shows var::1, why and how?
[414 byte] By [
skSuresha] at [2007-10-3 11:05:44]

That code won't compile. Please post the actual code.When you post code, please use[code] and [/code] tags as described in [url= http://forum.java.sun.com/help.jspa?sec=formatting]Formatting tips[/url] on the message entry page. It makes it much easier to read.
jverda at 2007-7-15 13:28:23 >

The reason, however, is that display is implemented in A, and hence only knows about A's member variables.
jverda at 2007-7-15 13:28:23 >

Even when the constructors of Class A called first and Class B called second, however display method is in Class A and the variable var is known to only for Class A. So class A var is displayed as 1. If you want to have it to the value 2, then put the same display() method in Class B.
class A
{
int var;
A()
{
var = 1;
System.out.println("A constructor");
}
public void display()
{
System.out.println("var :: "+ var);
}
public static void main(String wrg[])
{
B obj = new B();
obj.display();
}
}
class B extends A
{
int var;
B()
{
var = 2;
System.out.println("B constructor");
}
public void display()
{
System.out.println("var :: "+ var);
}
}
Sorry for didn't follow the format rules.When we are inheriting that method to B, then it becomes the member of B, then why it prints the A's var.
> Sorry for didn't follow the format rules.
>
> When we are inheriting that method to B, then it
> becomes the member of B, then why it prints the A's
> var.
So when you inherit to B, why do you want to declare variable var again in B. If you remove declaring variable in B, you will get 2.
> > Sorry for didn't follow the format rules.
> >
> > When we are inheriting that method to B, then it
> > becomes the member of B, then why it prints the
> A's
> > var.
>
> So when you inherit to B, why do you want to declare
> variable var again in B. If you remove declaring
> variable in B, you will get 2.
It's okay, when I hide the fields in class B, it has the method display derived from A, then why is it so?
Variable var and display() will be accessible in Class B when you inherit from A. I don't understand what you are asking exacltly?
When you refer to an instance method, like display(), which class' version of that method is invoked gets determined at compile time, and it's based on the type of the actual class--the runtime type. This is Java's runtime polymorphism.
The only members that are polymorphic this way are those that meet all of the folowing criteria:
* They are instance (non-static) members.
* They are methods (not variables).
* They are not private.
* They are not final.
For all the rest--any member that's static or a variable or private or final--which class' version of that member gets used is determined at compile time, by the type of the reference. In our case, it's a variable, and the reference is the implied "this," which is of type A, so A's variable is the one that's used.
jverda at 2007-7-15 13:28:23 >

display method became the member of class B, then why it is not acess the B's variable var?
Read my previous reply closely.
display refers to the variable a. Since a is a variable, whose a we're refererring to is determined at compile time, by the type of the reference. The compile-time type of the reference is A, because the method is declared in A so "this" is a reference of type A. Since "this" if of type A, it's A's variable.
jverda at 2007-7-15 13:28:23 >

Yes. Display() method would be known to the compiler at the compile time because of no display method declared in sub class to put the signature at run time to consider the variable in Class B. Class A's variable would be considered and known to the compiler because of having display() method in only in super class.
Thanks guys.....I got the point....