Encapsulatie & access modifiers

Introductie tot objectgeoriënteerd programmeren in Java

Sani Yusuf

Lead Software Engineering Content Developer

Encapsulatie begrijpen

  • Encapsulatie kun je uitleggen met een mobiele telefoon
  • Je hebt alleen toegang tot de zichtbare functies: scherm, speaker, enz.
  • De interne werking is volledig verborgen voor gebruikers

Mobiele telefoon

Introductie tot objectgeoriënteerd programmeren in Java

Public properties

  • Met public gemarkeerde properties zijn toegankelijk via objectinstanties

  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
        }
      }

Introductie tot objectgeoriënteerd programmeren in Java

Private properties

  • Met private gemarkeerde properties zijn toegankelijk via objectinstanties
  • Met private gemarkeerde properties kun je alleen binnen de class gebruiken

  // 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
        }
      }


Introductie tot objectgeoriënteerd programmeren 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;
      }    
  }    

Introductie tot objectgeoriënteerd programmeren 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(); } }
Introductie tot objectgeoriënteerd programmeren in Java

Static methods

  • Met static gemarkeerde properties zijn toegankelijk zonder objectinstantie
  • Meestal gebruikt voor gedeelde code/bibliotheken

   // 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
      }
  }

Introductie tot objectgeoriënteerd programmeren in Java

Samenvatting

  • Access modifiers: public, private, static
  • Access modifiers helpen applicaties te beveiligen
  • static maakt gebruik van classes zonder instanties mogelijk
Introductie tot objectgeoriënteerd programmeren in Java

Laten we oefenen!

Introductie tot objectgeoriënteerd programmeren in Java

Preparing Video For Download...