-
The basic fundamental of OOPS is the child class instance can access the properties and methods of parent class.
2.How child class instance can access properties and functions of base class without creating base class??
Ans :: Creating instance of child class (B B1 = new();) meaning calling new function of child class, in the new() function of child class super.new() gets called implicitly meaning memory allocation happened for base class properties, then child class properties gets memory allocation and now new() returns a pointer which points to the contents of both base and child class. This is the reason why child class instance can access parents properties or methods. -
A class type can hold a handle corresponding to its type only.
coming to your example
1.initial
2.begin
3.A1 = new();
4.A1.disp();
5.B1 = new();
6.A1 = B1;
7.A1.disp();
8.end
9.endprogram
Line no. 6, Handle of Base class is assigned to A1 meaning as A1 is class type variable of class A, it can only hold pointer to that particular type . As already mentioned handle of B1 points to both properties of A and B classes.
A1.disp() will call parent class method as A1 can only hold handle of class type A it can’t hold pointer to class B properties.