-
premier exemple
class A {
int m (A x) { System.out.println(1+" "); return (1);}
boolean n (A x) {System.out.println(2+" "); return (true);}
int m (A x, A y) {System.out.println(3+" "); return 3;}
boolean n (A x, A y) {System.out.println(4+" "); return (false);}
}
class B extends A {
int m (B x) { System.out.println(5+" "); return (5);}
boolean n (B x) {System.out.println(6+" "); return (true);}
int m (B x, B y) {System.out.println(7+" "); return 7;}
boolean n (B x, B y) {System.out.println(8+ " "); return (false);}
}
class ex1 {
public static void main(String [] args) {
A a1 = new A ();
B b1 = new B();
A a2 = b1;
System.out.println("*1-------");
a1.m(a1);
a1.n(a1);
a1.m(a1,a1);
a1.n(a1,a1);
System.out.println("*2-------");
b1.m(b1);
b1.n(b1);
b1.m(b1,b1);
b1.n(b1,b1);
System.out.println("*3-------");
b1.m(a1);
b1.n(a1);
b1.m(a1,a1);
b1.n(a1,a1);
System.out.println("*4-------");
a2.m(a1);
a2.n(a1);
a2.m(a1,a1);
a2.n(a1,a1);
System.out.println("*5-------");
a2.m(a2);
a2.n(a2);
a2.m(a2,a2);
a2.n(a2,a2);
System.out.println("*6-------");
b1.m(a1);
b1.n(b1);
b1.m(a1,b1);
b1.n(a1,b1);
}
}
- sortie du premier exemple
*1-------
1
2
3
4
*2-------
5
6
7
8
*3-------
1
2
3
4
*4-------
1
2
3
4
*5-------
1
2
3
4
*6-------
1
6
3
4
- deuxième exemple : dépendance croisée
class A {
int m (A x) { System.out.println(1+" "); return (1);}
boolean n (B x) {System.out.println(2+" "); return (true);}
int m (A x, A y) {System.out.println(3+" "); return 3;}
boolean n (B x, B y) {System.out.println(4+" "); return (false);}
}
class B extends A {
int m (B x) { System.out.println(5+" "); return (5);}
boolean n (A x) {System.out.println(6+" "); return (true);}
int m (B x, B y) {System.out.println(7+" "); return 7;}
boolean n (A x, A y) {System.out.println(8+ " "); return (false);}
}
class ex2 {
public static void main(String [] args) {
A a1 = new A ();
B b1 = new B();
A a2 = b1;
System.out.println("*1-------");
a1.m(a1);
a1.n(b1);
a1.m(a1,a1);
a1.n(b1,b1);
System.out.println("*2-------");
b1.m(b1);
b1.n(b1);
b1.m(b1,b1);
b1.n(b1,b1);
System.out.println("*3-------");
b1.m(a1);
b1.n(a1);
b1.m(a1,a1);
b1.n(a1,a1);
System.out.println("*4-------");
a2.m(a1);
a2.n(b1);
a2.m(a1,a1);
a2.n(b1,b1);
System.out.println("*5-------");
a2.m(a2);
a2.n((B)a2);
a2.m(a2,a2);
a2.n((B)a2,(B)a2);
System.out.println("*6-------");
b1.m(a1);
b1.n(b1);
b1.m(a1,b1);
b1.n(a1,b1);
}
}
- sortie du deuxième exemple
*1-------
1
2
3
4
*2-------
5
2
7
4
*3-------
1
6
3
8
*4-------
1
2
3
4
*5-------
1
2
3
4
*6-------
1
2
3
8
- troisième exemple
class A {
int m (A x) { System.out.println(1+" "); return (1);}
boolean n (B x) {System.out.println(2+" "); return (true);}
int m (A x, A y) {System.out.println(3+" "); return 3;}
boolean n (A x, B y) {System.out.println(4+" "); return (false);}
}
class B extends A {
int m (B x) { System.out.println(5+" "); return (5);}
boolean n (A x) {System.out.println(6+" "); return (true);}
int m (B x, B y) {System.out.println(7+" "); return 7;}
boolean n (B x, A y) {System.out.println(8+ " "); return (false);}
}
class ex3 {
public static void main(String [] args) {
A a1 = new A ();
B b1 = new B();
A a2 = b1;
System.out.println("*1-------");
a1.m(a1);
a1.n(b1);
a1.m(a1,a1);
a1.n(b1,b1);
System.out.println("*2-------");
b1.m(b1);
b1.n(b1);
b1.m(b1,b1);
b1.n(b1,b1);
System.out.println("*3-------");
b1.m(a1);
b1.n(a1);
b1.m(a1,a1);
b1.n(b1,b1);
System.out.println("*4-------");
a2.m(a1);
a2.n(b1);
a2.m(a1,a1);
a2.n(b1,b1);
System.out.println("*5-------");
a2.m(a2);
a2.n((B)a2);
a2.m(a2,a2);
a2.n((B)a2,(B)a2);
System.out.println("*6-------");
b1.m(a1);
b1.n(b1);
b1.m(b1,b1);
b1.n(b1,b1);
}
}
- sortie du troisième exemple
ex3.java:31: reference to n is ambiguous, both method n(A,B) in A and method n(B,A) in B match
b1.n(b1,b1);
^
ex3.java:36: reference to n is ambiguous, both method n(A,B) in A and method n(B,A) in B match
b1.n(b1,b1);
^
ex3.java:51: reference to n is ambiguous, both method n(A,B) in A and method n(B,A) in B match
b1.n(b1,b1);
^
3 errors
- cas d'ambiguité
class A {
int m (A x) { System.out.println(1+" "); return (1);}
int m (A x, A y) {System.out.println(3+" "); return 3;}
}
class B extends A {
int m (B x) { System.out.println(5+" "); return (5);}
int m (A x, B y) {System.out.println(7+"bis "); return 7;}
int m (B x, A y) {System.out.println(7+"ter "); return 7;}
}
class ex4 {
public static void main(String [] args) {
A a1 = new A ();
B b1 = new B();
A a2 = b1;
System.out.println("*1-------");
a1.m(a1);
a1.m(a1,a1);
System.out.println("*2-------");
b1.m(b1);
b1.m(b1,b1);
System.out.println("*3-------");
b1.m(a1);
b1.m(a1,a1);
System.out.println("*4-------");
a2.m(a1);
a2.m(a1,a1);
System.out.println("*5-------");
a2.m(a2);
a2.m(a2,a2);
System.out.println("*6-------");
b1.m(a1);
b1.m(b1,b1);
}
}
- sortie de la compilation
ex4.java:24: reference to m is ambiguous, both method m(A,B) in B and method m(B,A) in B match
b1.m(b1,b1);
^
ex4.java:36: reference to m is ambiguous, both method m(A,B) in B and method m(B,A) in B match
b1.m(b1,b1);
^
2 errors