F
FET
Hi everyone, I have a slight problem understanding the following
behaviour. I am citing 3 classes below:
class A{
public int c;
A(){
c = 5;
}
public void f(){
System.out.println("Inside A");
}
}
class B extends A{
public int c;
B(){
c = 10;
}
public void f(){
System.out.println("Inside B");
}
}
class Main{
public static void main(String args[]){
A a = new A();
B b = new B();
a = b;
System.out.println("C = "+a.c);
a.f();
}
}
On running Main, I get the output as:
C=5
Inside B
Even though I equate Base Class reference to Derived class reference,
Why is it that the data members of the Base class are accessed ?
Thanks in advance.
Regards.
behaviour. I am citing 3 classes below:
class A{
public int c;
A(){
c = 5;
}
public void f(){
System.out.println("Inside A");
}
}
class B extends A{
public int c;
B(){
c = 10;
}
public void f(){
System.out.println("Inside B");
}
}
class Main{
public static void main(String args[]){
A a = new A();
B b = new B();
a = b;
System.out.println("C = "+a.c);
a.f();
}
}
On running Main, I get the output as:
C=5
Inside B
Even though I equate Base Class reference to Derived class reference,
Why is it that the data members of the Base class are accessed ?
Thanks in advance.
Regards.