Java: How does a method operate on objects of unknown type?
<p>Consider, the following three Java classes in three different packages:</p>

<p><strong>User.java</strong></p>

<pre><code>package user;

public class User {

    String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User [name=" + name + "]";
    }
}
</code></pre>

<p><strong>UserFactory.java</strong></p>

<pre><code>package factory;

import user.User;

public class UserFactory {

    private static User user;

    public static User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

}
</code></pre>

<p><strong>Test.java</strong></p>

<pre><code>package test;

import factory.UserFactory;

public class Test {
    public static void main(String[] args) {
        UserFactory.getUser().setName("StackOverflow");
        System.out.println(UserFactory.getUser());
    }
}
</code></pre>

<p>The main method belongs to class <code>test.Test</code> that doesn't import <code>user.User</code> class. However, it is able to operate on <code>user.User</code> type. </p>

<p>How to justify this behaviour? </p>