I got one Interface and one Class.
public interface IUser{
public String getFirstname();
public String getLastname();
}
public class User implements Iuser{
String firstname;
String lastname;
public User(String firstname, String lastname)
{
this.firstname = firstname;
this.lastname = lastname;
}
public String getFirstname(){
return firstname;
}
public String getLastname(){
return lastname;
}
}
When I usually need to create an instance of an User
I would do:
User user = new User("Mike", "L");
But I am confused with articles that I found on the internet, because they are creating an instance of an User-object like this:
IUser user = new User("Mike", "L");
I would like to know why programmers are creating an User
instance with the Interface in front of it?
What is the reason of creating an instance in this way?
Why would you prefer IUser user = new User()
instead of User user = new User()
?