Class methods

Introduction to Object-Oriented Programming in Java

Sani Yusuf

Lead Software Engineering Content Developer

Example of car properties

  • Cars can have colors.
  • Each car has a model.
  • Cars can also perform actions like go on/off.
  • Cars can also give us data like a list of devices paired with the car's Bluetooth.

Car Colours

Car Model

Turn Car On

Introduction to Object-Oriented Programming in Java

Types of methods

  • Void methods: Methods that do not return data
  • Non-Void methods: Returns data back
Introduction to Object-Oriented Programming in Java

Void methods

  • Void methods do not return any data
  • They are marked with the keyword void

 // Car class
 class Car {

     void honkHorn() {
      // Code for horn to sound
       System.out.println("beep beep");
      }
 }


$$ $$


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

      Car myNewCar = new Car(); 
      myNewCar.honkHorn(); // beep beep
    }
  }


Introduction to Object-Oriented Programming in Java

Creating non void methods

  • Non void methods always have a return type
  • Non void methods use the return keyword to return data

 // Formula class
 class Formula {


// Non void method int getSquare(int number) { // Return an "int" value return number * number; } }

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

      Formula myFormula = 
        new Formula(); 
      // Calling "getSquare" method
      System.out.println(
        myFormula.getSquare(5)); // 25
    }
  }

Introduction to Object-Oriented Programming in Java

Let's practice!

Introduction to Object-Oriented Programming in Java

Preparing Video For Download...