| [Précédent] |
| 9.5 |
abstract et final - Corrigé | Niveau 1 |
||
Fichiers: |
AbstractFinal.java | |||
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 |
class AbstractFinal {
public static void main(String[] args) {
A[] tab = new A[3];
// Il n'est pas possible d'instancier une classe abstraite
// tab[0] = new A(); // Faux
tab[1] = new B(); // OK
tab[2] = new C(); // OK
// il faudrait caster, ((B)tab[1]).b = 2:
// tab[1].b = 2; // Faux
// Il n'est pas possible de modifier une variable finale:
// ((C)tab[2]).c = 3; // Faux
}
}
abstract class A {
int a;
}
class B extends A {
int b;
}
class C extends A {
final double c = 1;
}
abstract class D extends A {
double d;
int operation(int a) {
return (a * 2);
}
// Une méthode abstraite ne peut pas avoir d'instructions ou
// d'accolades, seulement un point-virgule:
abstract int calcul(int b);
// { } // Faux
abstract int machin();
} |
| [Précédent] |