Introduction to polymorphism

Introduction to Object-Oriented Programming in Java

Sani Yusuf

Lead Software Engineering Content Developer

Demonstrating polymorphism

  • Polymorphism is a concept in Object-Oriented Programming (OOP) that allows objects to exist in multiple forms
  • Showcasing class inheritance

  // 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 to Object-Oriented Programming in Java

Overriding methods

  • The @Override keyword allows you to create custom method implementations
  // 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 to Object-Oriented Programming in Java

Overriding interface methods


  // ElectricCar interface
  interface ElectricCar {

    abstract void charge(); 
    // No Code Implementation 

  }


  • @Override is recommended when using interface methods

  // 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 to Object-Oriented Programming in Java

Overloading methods

  • Overloading allows methods with the same name, to have different implementations

  // Toyota class 
  class Toyota {   

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

// Overloaded drive method void drive(int topSpeed) { // Second implementation of drive } }
Introduction to Object-Oriented Programming in Java

Overloading the constructor

  • Constructors can be overloaded to have multiple in the same class
  // Honda class
  class Honda {
    // First Constructor
    public Toyota(String color,
                 String model) {

    }

// Second Constructor public Toyota(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 to Object-Oriented Programming in Java

Let's practice!

Introduction to Object-Oriented Programming in Java

Preparing Video For Download...