when an object of subclass is assigned to the variable of a super class,why only those members are accessible which are defined by the superclass
class A {
int i=10;
void adsip() {
System.out.println(i);
}
}
class B extends A {
int j=20;
void bdsip() {
System.out.println(i+j);
}
}
class inherit4 {
public static void main(String[] x) {
A a=new A();
B b=new B();
System.out.println("b.i="+b.i+"b.j="+b.j);
b.adsip();
b.bdsip();
a=b;
System.out.println("a.i="+a.i);
a.adsip();
}
}
ABOVE CODE IS WORKING FINE but after adding a.j and a.bdisp(); error is generated,as far as i know a & b in the above code represent refrence to memory allocation of objects of class A & B then why code is not able to access a.j and a.bdsip(); in above code.