Overloading and Overwriting

I was looking at the last example at Lecture 17. A question popped up in my mind.

Let’s say we have the declarations as follows:

class A  {
   void m(boolean);
class B extends A {
   void m(int);
   void m(boolean);

and we run the code following:

A a = new B();

which function would be called?
Since the function with int type in class B is overloading, it should be considered if our static type were B. However, our static type is A in this case.
Moreover, we do not have a proper function to call in A
I thought we would have an error as far as I understand.

Thanks in advance.

The code does not compile, as expected. So no method is called.

1 Like

I guess you mean

class A { ... }
class B extends A { ...}

In that case, you’re right for the reason you mention.


Yes, I meant that, sorry for the mistake. Thank you for the answer. :slightly_smiling_face: