Introduction au polymorphisme

Introduction à la programmation orientée objet en Java

Sani Yusuf

Lead Software Engineering Content Developer

Démonstration du polymorphisme

  • Le polymorphisme, en programmation orientée objet (POO), permet à des objets d'exister sous plusieurs formes
  • Illustration de l'héritage de classes

  // Abstract Car class
  abstract class Car {

    // Abstract drive method
    abstract void drive(); 
    // No Code Implementation 

  }

  // Toyota class inherits Car class
  class Toyota extends Car {  

  }
  // Tesla class inherits Car class
  class Tesla extends Car {  

  }
  // Lamborghini class inherits Car class
  class Lamborghini extends Car {          

  }
Introduction à la programmation orientée objet en Java

Redéfinir des méthodes

  • Le mot-clé @Override permet de créer des implémentations personnalisées de méthodes
  // Abstract Car class
  abstract class Car {

    // Abstract drive method
    abstract void drive(); 
    // No Code Implementation 

  }


 class Toyota extends Car {  
   @Override // Keyword used to override
    void drive() {
      // Toyota specific implementation code
    }  
 }
 class Tesla extends Car {   
   @Override
    void drive() {
      // Tesla specific implementation code
    } 
 }
 class Lamborghini extends Car {  
   @Override
    void drive() {
      // Lamborghini specific implementation code
    }   
 }
Introduction à la programmation orientée objet en Java

Redéfinir des méthodes d'interface


  // ElectricCar interface
  interface ElectricCar {

    abstract void charge(); 
    // No Code Implementation 

  }


  • @Override est recommandé avec les méthodes d'interface

  // Tesla class implementing 
  // ElectricCar interface
  class Tesla implements ElectricCar {

    // charge method must be implemented
    @Override
    void charge(){
      // Tesla specific charge implementation
      // Selective inheritance with interface
    }          
  }













Introduction à la programmation orientée objet en Java

Surcharger des méthodes

  • La surcharge permet d'avoir des méthodes du même nom avec des implémentations différentes

  // Toyota class 
  class Toyota {   

    void drive() {
      // First implementation of drive method
    }

// Overloaded drive method void drive(int topSpeed) { // Second implementation of drive } }
Introduction à la programmation orientée objet en Java

Surcharger le constructeur

  • Les constructeurs peuvent être surchargés : plusieurs dans la même classe
  // Honda class
  class Honda {
    // First Constructor
    public Honda(String color,
                 String model) {

    }

// Second Constructor public Honda(String color, String model, String licensePlate) { } }

  public class Main {  
    public static void main(
      String[] args) {

      // First Constructor usage
      Honda hondaOne = 
        new Honda("Red", "Accord");


// Second Constructor usage Honda hondaTwo = new Honda("Red", "Civic", "FST-1977"); } }
Introduction à la programmation orientée objet en Java

Passons à la pratique !

Introduction à la programmation orientée objet en Java

Preparing Video For Download...