Making the car class abstract

Introduction to Object-Oriented Programming in Java

Sani Yusuf

Lead Software Engineering Content Developer

Explaining abstract class

  • The phrase Car only exists as a placeholder
  • We still need to know more like what type of car it is

Toyota

Mercedes

Introduction to Object-Oriented Programming in Java

Food analogy for abstract class

  • You never ask for just food at a restaurant
  • You always specify what type of food you want to eat
    • bread
    • pasta
    • rice

Bread

Pasta

Rice

Introduction to Object-Oriented Programming in Java

Understanding abstract classes

  • Abstract classes exist to hold features for inheriting classes
  • Abstract classes cannot have object instances created
  • Abstract classes generally do not have code implementation
Introduction to Object-Oriented Programming in Java

Car abstract class


  // Car abstract class
  abstract class Car {    
    // abstract drive method
     abstract void drive(); // No Code Implementation 

  }








Introduction to Object-Oriented Programming in Java

Abstract methods

  • Abstract method have no implementation
  • The class that inherits, must implement abstract methods

   // Car abstract class
  abstract class Car {    
    // abstract drive method
     abstract void drive(); // No Code  
                            // implementation
  }





  // Toyota class 
  class Toyota extends Car {  
     void drive(){
      // Toyota drive() implementation
    }           
  }
  // Porsche class
  class Porsche extends Car {  
     void drive(){
      // Porsche drive() implementation
    }           
  }
Introduction to Object-Oriented Programming in Java

Creating concrete methods

  • Concrete methods are the methods with code implementation
  • Subclasses do not need to implement concrete methods

  // abstract Car class
  abstract class Car {
    private int topSpeed;

    // Concrete method with implementation
    public getTopSpeed(){
         return this.topSpeed;
    }

    abstract void drive(); 
    // No Code Implementation        
  }

Introduction to Object-Oriented Programming in Java

Let's practice!

Introduction to Object-Oriented Programming in Java

Preparing Video For Download...