I have class A and its child class B.

What is the difference between writing:

А а1 = new A()

and

 А а1 = B() 

    2 answers 2

    In both cases, you declare a reference variable of type A , but in the second case you assign a reference to object B to this variable.

    In this case, in both cases, you can only call the methods described in class A But, if their behavior is redefined in B , then in the second case, the implementation from class B will be called, since the object is exactly B

      In the second case, we will not be able to access the unique methods of class B, but if some methods override the corresponding ones from class A, then the redefined implementation will work.

      • Thanks for the help - Oleg