Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
    class Alpha
    {
         public void afficher(double d)
         {
              System.out.println("renvoyer double de alpha : " + d);
         }

    }

    class Beta extends Alpha
    {

    }

    class Gamma extends Alpha
    {
         public void afficher(long l)
         {
              System.out.println("renvoyer long de gamma : " + l);
         }
    }

    class Delta extends Gamma
    {
         public void afficher(int i)
         {
              System.out.println("renvoyer int de delta : " + i);
         }
    }

    class Omega extends Gamma
    {
         public void afficher(float f)
         {
             System.out.println("renvoyer float de omega : " + f);
         }

         public void afficher(int i)
{
              System.out.println("renvoyer int de omega : " + i);
         }
    }




public class TesterSurdefinitionETpolymorphisme

    {
         public static void main(String [] args)
         {
             byte b = 1; short s = 5; int i = 22; long l = 44;
            float f = 2.5f; double d = 33.01;

              Alpha a = new Alpha();
              a.afficher(b);
              a.afficher(f);
              System.out.println();

                a.afficher(b);
            a.afficher(f);
            System.out.println();

            Gamma g = new Gamma();
            g.afficher(b);
            g.afficher(l);
            g.afficher(f);

       a = g; 
   a.afficher(b);
    a.afficher(l);
    a.afficher(f);
            System.out.println();

    a = g;       
           a.afficher(b);
           a.afficher(l);
           a.afficher(d);
            System.out.println();

            Omega o = new Omega();
            o.afficher(b);
            o.afficher(i);
            o.afficher(f);
            o.afficher(d);

       //     a = 0;
         a.afficher(b);
         a.afficher(i);
         a.afficher(f);
         a.afficher(d);

            g = o;
            g.afficher(b);
            g.afficher(i);
            g.afficher(f);
            g.afficher(d);
        }
   }
Bonjour à tous,

Je n'arrive pas à comprendre un point dans le polymorphisme. J'ai mis en gras une ligne dans le code ci-dessus, si quelqu'un pourrait t-il m'expliquer le comportement ?

Le résultat obtenu est (les 3 instructions après la ligne en gras):

renvoyer double de alpha : 1.0
renvoyer double de alpha : 44.0
renvoyer double de alpha : 33.01
D'avance, je vous remercie de m'aider à la compréhension de toutes les subtilités.