Encapsulation & access modifiers

Introduction to Object-Oriented Programming in Java

Sani Yusuf

Lead Software Engineering Content Developer

Understanding encapsulation

  • Encapsulation can be explained with a mobile device
  • You only have access to dedicated exposed features like screen, speaker, etc
  • The inner workings are entirely hidden from users

Mobile Phone

Introduction to Object-Oriented Programming in Java

Public properties

  • Properties marked public can be accessed by object instances

  class Car {
    // Public property
    public String color;


// Public constructor public Car(String color){ this.color = color; } }

    public class Main {

      // main method
      public static void main(String[] args) {
          Car myCar = new Car("brown");
          // Can access public members 
          // from object instance
          System.out.println(
            myCar.color); // Brown
        }
      }

Introduction to Object-Oriented Programming in Java

Private properties

  • Properties marked private can be accessed by object instances
  • Properties marked private can only be used within the class

  // Car class
  class Car {
     public String color;

// Private properties private String model;
public Car(String color){ this.color = color; } }

    // Main class
    public class Main {
      // main method
      public static void main(String[] args) {
          Car myCar = new Car("brown");
          // Calling private properties causes errors        
          System.out.println(
            myCar.model); // Java compilation error
        }
      }


Introduction to Object-Oriented Programming in Java

Public methods


  // Car class
  class Car {
      public String color;
      private String model;

      public Car(String color){
          this.color = color;
      }

      // Public method
      public String getModel(){
          return this.model;
      }    
  }    

Introduction to Object-Oriented Programming in Java

Private methods


  class Car {


// Private method, "calculateSpeed" can only be used within "Car" class private calculateSpeed(){ // Trademarked formula code } public int getSpeed(){ // "calculateSpeed" can be used anywhere within "Car" class return this.calculateSpeed(); } }
Introduction to Object-Oriented Programming in Java

Static methods

  • Properties marked static can be accessed without an object instance
  • Typically used to house shared code/libraries

   // Formula class 
   static class Formula {

     // Method for calculating square
     static int getSquare(int number) {
        return number * number;
      }
   }





  // Main class 
  public class Main {

    public static void 
      main(String[] args) {
          // We can use "getSquare" 
          // without an object instance
        System.out.println(
          Formula.getSquare(5)); // 25
      }
  }

Introduction to Object-Oriented Programming in Java

Recap

  • Access modifiers: public, private, static
  • Access modifiers help secure applications
  • static enables the use of classes without creating instances
Introduction to Object-Oriented Programming in Java

Let's practice!

Introduction to Object-Oriented Programming in Java

Preparing Video For Download...