21:43
@genaray the object instance that c
is pointing to, is by no means an AA
but independent of what it is assigned to, it at least satisfies the contract of AA
so, the object will have all methods that the AA
class defined
therefor, you can safely call the foo1
and foo2
functions on it
the class BB
overrides foo2
originally declared in class AA
therefor, instances of class BB
, on which you call the foo2
method, will use the BB
version
independent of what the variable tells you what type the instance is
so, AA c = new BB();
in this case, we are dealing with an object that satisfies the AA
contract (as defined by the variable c
)
but the actual object is BB
so, we can call foo2
and it will run the foo2
method from BB
and print out "B"
to the console
the foo1
is slightly more annoying
BB::foo1
does not override AA::foo1
instead, it creates an overload
if you are dealing with a variable declared as AA
(for example, our c
variable)
then you can only see and use AA::foo1
it is not overridden, so the function AA::foo1
will be called (not BB::foo1
)
theferor, c.foo1(any)
will print "A"
if the variable is declared as BB
(for example, our b
variable)
then you can see and use both AA::foo1
and BB::foo1
if you call b.foo1(any)
, the compiler will look at any
and find out what type it is
if any
is declared as AA
(for example, our c
variable), then it will use AA::foo1
and not BB::foo1
because the compiler cannot guarantee that a variable declared as AA
will actually hold a BB
pointer
if any
is declared as BB
(for example, our b
variable), then the compiler will say "hey, AA::foo1
is fine for this call, but I have an overload that is even better, which is BB::foo1
"
in that case, BB::foo1
will be used
on a side note, if you have an exam tomorrow... you might have started studying too late... especially if you have no clue (your words) about the stuff that you need to learn
@SumanthVarada what Zoe is trying to say is "oh hey, that looks like an interesting problem. however, because we dont really know what you really want, we cant really help you. please explain what you are trying to achieve and ask for the specific thing you need to achieve it, best regards, Zoe"