java (encapsulation)

Encapsulation

-Prevents illegal values from entering fields ・ Create a "consistent class" ・ Directly connected to the object-oriented essence of "faithfully imitating the real world"

Restrict access to fields and methods

** Access modifier **

name How to specify Scope of permission for access
private private Only my own class
package private (Do not write anything) Classes that belong to the same package as you
protected protected Classes of children who belong to the same package as you or inherit from you
public public All classes

Access modifiers for members

・ All fields are private ・ All methods are public

getter and setter

-Field manipulation via methods

getter -"Get" + "field name capitalized" -A method that simply calls the contents of the name field and returns it

Hero.java


public class Hero {
  private String name;

  public String getName() {  //getName()Access the name field via
    return this.name;
  }
}

King.java


public class King {
  void talk(Hero h) {
    System.out.println("King: Welcome to our country, brave man" + h.getName() + "Yo.");
  }
}

setter -"Set" + "Field name with capital letters at the beginning" -Methods that just assign values (in other classes)

Hero.java


public class Hero {
  private String name;

  public void setName(String name) {
    this.name = name;    //this.Never forget
  }
}

Access control for classes

name How to specify Scope of permission for access
package private (Do not write anything) Classes that belong to the same package as you
public public All classes

Features of non-public (package private) classes

-The class name may be different from the source file name -Multiple classes may be declared in one source file

Hero.java


public class Hero {
}
class HeroSkill {
}
class HeroSinbol {
}

Hero.java


class Character {
}
class Boss {
}

Recommended Posts

java (encapsulation)
[Java] Encapsulation
What is Java Encapsulation?
Java
Encapsulation
Java
Java encapsulation private public
Java encapsulation and getters and setters
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java array
Studying Java ―― 9
Java scratch scratch
Java tips, tips
Java methods
java (constructor)
Java array
[Java] ArrayDeque
java (override)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
Encapsulation review
java beginner
Java (add2)
JAVA (Map)
[java] interface
Java9 collection
Java basics