Adding properties

Introduction to Object-Oriented Programming in Java

Sani Yusuf

Lead Software Engineering Content Developer

Car example

  • Cars on the road can come in different forms, with features like color and model that differentiate them.

Cars

Introduction to Object-Oriented Programming in Java

Differentiate cars

  • Even when cars are the same, they differ by color, license plate, vehicle registration.

Same Cars

Introduction to Object-Oriented Programming in Java

Adding properties to class

  • Properties describe a class
  • Properties can have different data types

  // Car class
  class Car {
    String model; // Property for model of car

int topSpeed; // Property for car's top speed boolean isInsured; // Property for current insurance state }
Introduction to Object-Oriented Programming in Java

Constructors in Java

  • The Constructor is a method, always called when an object instance of a class is created
  • Constructor must have the same name as the class it is in

  class Passport {
    String firstName; // Passport holder's first name
    String lastName; // Passport holder's last name

    Passport() {
      // Constructor of Passport class
    }
  }


Introduction to Object-Oriented Programming in Java

Setting properties inside the constructors


  class Passport {
    String firstName; // Passport holder's first name
    String lastName; // Passport holder's last name

    Passport() {
      this.firstName = "David";  // Set property inside constructor
      this.lastName = "Beckham";
    }
  }


Introduction to Object-Oriented Programming in Java

Constructors with parameters

  • Constructors can have parameters just like any other method

  class Passport {
    String firstName;
    String lastName;

    // Constructor with parameters
    Passport(String firstName, String lastName) {  

    }
  }


Introduction to Object-Oriented Programming in Java

Setting properties with constructor parameters

  • The this keyword refers to the Passport object
  • It is common to give constructor parameters same name as class properties

  class Passport {
    String firstName;
    String lastName;

    Passport(String firstName, String lastName) {
          this.firstName = firstName; // Setting class properties
          this.lastName = lastName;   //  with constructor parameters
    }
  }


Introduction to Object-Oriented Programming in Java

Creating object instances with parameters

  • Objects are created with the new keyword
  • Constructor parameters are passed during object creation

  // Passport Class with constructor
  class Passport {
    String firstName;
    String lastName;

    // Constructor method
    Passport(String firstName, 
             String lastName){
          this.firstName = firstName;
          this.lastName = lastName;
    }
  }





  // Main Class    
  public class Main {  
    // main method (program entry point)
    public static void main(
      String[] args) {

      // Passing parameters to constructor 
      // Passport(firstName, lastName)
      Passport myPassport = 
        new Passport("Michael","Jackson"); 
      System.out.println(
        myPassport.firstName); // Michael
    }
  }


Introduction to Object-Oriented Programming in Java

Let's practice!

Introduction to Object-Oriented Programming in Java

Preparing Video For Download...