Encapsulation & access modifiers

Introduzione alla programmazione orientata agli oggetti 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

Introduzione alla programmazione orientata agli oggetti 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
        }
      }

Introduzione alla programmazione orientata agli oggetti 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
        }
      }


Introduzione alla programmazione orientata agli oggetti 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;
      }    
  }    

Introduzione alla programmazione orientata agli oggetti 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(); } }
Introduzione alla programmazione orientata agli oggetti 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
      }
  }

Introduzione alla programmazione orientata agli oggetti in Java

Recap

  • Access modifiers: public, private, static
  • Access modifiers help secure applications
  • static enables the use of classes without creating instances
Introduzione alla programmazione orientata agli oggetti in Java

Let's practice!

Introduzione alla programmazione orientata agli oggetti in Java

Preparing Video For Download...