posted 19 years ago
Hello all,
i had one doubt in this below program, i don't know how it is working in java compliler or how java treats this code line by line.
The Code is:
************
class Super
{
int i=5;
void display()
{
System.out.println("Hello Java:");
}
}
class Sub extends Super
{
int i=7;
void display()
{
System.out.println("Hai Javaranch:");
}
}
class Main
{
public static void main(String[] args)
{
Super S = Sub();
S.display();
System.out.println("The Value="+S.i);
}
}
*******************************
The above program will gives the output as,
Hai Javaranch
5
**********************************]
My question is, i had created the object for Super class and i had assigned it to Sub class.
while accessing it takes the method of Subclass method.
but, while accessing the variable it takes the Superclass variable.
i don't know why.
please any one explain the below line which is in the program also,
//Super S = new Sub();
*************************************************************************
Thanks in Advance,
Vijay