Confusion on call Java Interface method
interface, java
Solution
A aa = new AImpl();
aa.a();
Here your reference variable is interface `A` type But actual `Object` is `AImpl`.
When you define a new interface, you are defining a new reference data type. You can use interface names anywhere you can use any other data type name. If you define a reference variable whose type is an interface, any object you assign to it must be an instance of a class that implements the interface.
Read more on Documentation
A Interface reference can hold Object of AImpl as it implements the A interface.
Problem
Let's say I have an interface A, defined as follows: ``` public interface A { public void a(); } ``` It includes a method called a. I have a class which implements this interface and it has only one method: ``` public class AImpl implements A { @Override public void a() { System.out.println("Do something"); } } ``` Q: If, in the main class I call the interface method, will it call the implementation belonging to the class which implements the interface? For example: ``` public static void main(String[] args) { A aa; aa.a(); } ``` Will this print "Do something"?