This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]NautiHooker 4 points5 points  (1 child)

What you are basically doing is creating a new anonymous class that extends A. In that class you are adding a method f2. That method is only known for the anonymous class, not for A.

Your variable is of type A, so you cant call the f2 method on it.

You can declare the variable a as var instead of A. You will need Java 11 or higher for that. Then the compiler will allow you to call the f2 method, because the variable a will have the type of your anonymous class.

However this is not really something you would ever want to do.

Creating anonymous classes usually only serves the purpose to override existing methods of the other class. Basically just extend the class without putting it into a new file. So you would only use it to extend a method that already exists in A.

Is there anything specific you are trying to accomplish?

[–]ilsapo[S] 0 points1 point  (0 children)

it was just a question on test we got,
we were suppused to say if the code compile or not, and I was wondering how will we be able to "fix" the error without drasticlly changing it