Why a parent class cannot access a member of a child class

If we go according to below code

class A;
  int a = 10;
endclass

class B extends A;
  int b = 20;
endclass

program test;
  A a1;
  B b1;
  initial begin
    b1 = new();
    a1 = b1; //child class object is assigned to parent class handle
    $display("Value of variable b is %x", a1.b);
  end
endprogram

Then the above code leads to the error: “Could not find element“ b ”in class“ A ”

Now I observe that when an extended class object is assigned to a base class descriptor, then the simulator checks the type of the descriptor and checks whether a variable is present in this class or not. Because the variable b is not defined in the base class, it will lead to error.
So, I want to confirm the correctness or incorrectness of the above, my?

I would appreciate if someone wants to add something to my observation, if this is the right but.

thanks

+4
2

. , .

, , - , / . : a1 b1. , . , , , .

OOP , , . , , . . , .

0

, , ( , , ). , , - ( ).

Java, , . :

public A buildA () {
    return new B();
}
public static void main () {
    A a1 = new A();
    A b1 = buildA();
}

, a1 b1 . b, , , , , A, , .

+2

Source: https://habr.com/ru/post/1622212/


All Articles