Erreur sur l'implémentation d'une interface
Bonjour,
Je suis débutant en JAVA et je ne comprends pas l'erreur que j'ai faite dans mon code.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| interface Personne
{
Personne change(String t);
String telephone();
}
class Person implements Personne {
private String nom ;
private String prenom ;
private String tel ;
// Constructeur d'instances Person qui implémente, en même temps, le type Personne
Person(String n, String p, String t)
{
nom = n ;
prenom = p;
tel = t;
}
public Person change(String t)
{
tel = t;
return this;
}
public String telephone()
{
return tel;
}
public String toString()
{
return "(" + nom +" " + prenom +" : " + tel + ")";
}
}
// programme utilisateur de la classe Personne
public class MesAmis {
public static void main (String[] args) {
System.out.println("Creation de a1 et de a2 ");
try {
Personne a1 = new Person("Durand", "Paul", "34 75") ;
Personne a2 = new Person("Sinclair", "Jane", "35 45") ;
System.out.println("Mon ami a1 : " + a1);
System.out.println("Mon ami a2 : " + a2);
System.out.println("Telephone de a1 est : " + a1.telephone());
a1.change("25 48");
System.out.println("Maintenant, c'est : " + a1.telephone());
} catch(Exception e) { System.out.println("Attention : " + e);
}
System.out.println("FIN");
}
} |
En execution, j'ai le résultat suivant :
Exception in thread "main" Creation de a1 et de a2
Mon ami a1 : (Durand Paul : 34 75)
Mon ami a2 : (Sinclair Jane : 35 45)
Telephone de a1 est : 34 75
java.lang.AbstractMethodError: Person.change(Ljava/lang/String;)LPersonne;
at MesAmis.main(MesAmis.java:45)
Je ne comprends pas pourquoi eclipse veut que je mettes une variable de sortie de type Personne alors que pour moi la variable de sortie doit être de type Person.