객체의 멤버필드(고유한 속성), 멤버 메소드(고유한 기능)에 대한 정보나 기능을 객체의 외부에서 함부로 접근할 수 없게 제한
멤버필드, 멤버메소드에 적용하며, 종류에 따라 공개범위가 단계적으로 달라짐
private < default < protected < public
private 같은 객체 내부에서만 접근 가능
default 같은 패키지의 다른 클래스까지 접근 가능
protected 같은 패키지 + 상속 관계의 객체까지 접근 가능
public 모든 접근 가능
멤버필드 : 주로 private을 작성
멤버메소드 : 주로 public을 작성
클래스 정의코드 : public 또는 default만 가능
package java06_class;
public class Class_02 {
private int num1 = 100;
(default) int num2 = 200;
protected int num3 = 300;
public int num4 = 400;
-----------------------------------------------------
package java06_class.defaultTest; 👉🏻 패키지가 다름
public class Main_02_defaultTest {
public static void main(String[] args) {
Class_02 cl = new Class_02();
System.out.println( cl.num4 ); ➡ public
//System.out.println( cl.num3 ); ➡ protected //패키지가 달라져서 값을 가져올 수 없음
//System.out.println( cl.num2 ); ➡ default //패키지가 달라져서 값을 가져올 수 없음
//System.out.println( cl.num1 ); ➡ private
}
}
[접근제한자] [클래스식별자] class [클래스명] { //정의부, Head
//멤버필드
//멤버 메소드 //구현부, Body
//생성자
}
public 또는 default 두 가지만 적용할 수 있음
클래스의 용도에 맞는 특별 기능을 부여할 때 사용
abstract, final, static, ...
👉🏻 메소드 밖에서 선언된 것
객체가 수행할 수 있는 기능, 동작을 정의
객체를 생성할 때 반드시 호출하도록 정의해놓는 코드
초기화함초기화 : 변수의 기본값과 객체의 기능동작에 필요한 설정함수, Function + 객체지향 원리
[접근제한자] [제한자] [리턴타입] [메소드명] (매개변수) {
//실행코드
}
소문자로 시작메소드에 추가적인 역할, 기능을 부여할 때 사용
abstract, final, static, ...
메소드가 종료할 때 되돌려 주는 데이터(반환데이터, Return Data)의 자료형을 명시 👉🏻 메소드 출력 데이터
void라고 적음메소드 동작에 필요한 데이터를 저장하는 변수, 메소드 입력 데이터
👉🏻 입력할 매개변수가 없다면 () 괄호 안을 비워둠
ex) main(String[] args) 👉🏻 문자열 여러개가 필요
public void method() {
System.out.println("Hello");
}
public void method( int parameter ) {
System.out.println("전달된 값 : " + parameter);
}
public int method() {
return 123;
}
public int method(int n1, int n2) {
return n1 + n2;
}
Public class Method_01 {
public void method() {
System.out.println("메소드 호출 테스트");
}
//2개의 정수 뺄셈하는 메소드
public int sub(int num1, int num2) {
int result = num1 - num2;
return result;
}
--------------------------------------------------------
public class MethodEx { //Executor 실행기의 약자
public static void main(String[] args) {
//객체 생성
Method_01 m01 = new Method_01();
m01.method();
➡ 메소드 호출 테스트
System.out.println("뺄셈 결과 : " + m01.sub(3, 2));
➡ 1
}
객체지향프로그램의 특징 중 하나
✔ 결합도 : 객체끼리의 연결(의존)정도
✔ 응집도 : 객체 내부에 관련 사항들이 모여있는 정도
private로 두고, 필요시 공개 ➡ 정보 은닉, Information Hiding멤버필드의 값을 저장할 수 있도록 작성하는 메소드
public void setXxxx( [멤버필드의 자료형] [멤버필드명] ) {
this.[멤버필드명] = [멤버필드명];
}
ex)
private int data; //멤버필드
public void setData( int data ) {
this.data = data;
}
멤버필드의 값을 불러올 수 있도록 작성하는 메소드
public [멤버필드의 자료형] getXxxx() {
return [멤버필드명];
}
ex)
private int data; //멤버필드
public int setData() {
return data;
}
alt + s, ralt + shift + s, r같은 이름의 메소드를 여러 개 중복정의 하는 것
💡 매개변수만 다르면 됨
public void test() { }
vs 👉🏻 오버로딩❌ / 반환타입은 오버로딩의 조건이 되지 않음
public int test() {
return 0;
}
--------------------------------------------------------------------
public void test(String str, int i) { }
vs 👉🏻 오버로딩⭕ / 매개변수의 위치가 다르면 중복정의가 아님
public void test(int i, String str) { }
멤버 필드, 멤버 메소드와 함께 클래스를 구성하고 있는 요소
생성자의 이름 = 클래스 이름
[접근제한자] 클래스명 ( 매개변수 ) {
//객체를 생성하면서 수행할 작업(코드)
}
클래스의 내부 구조, 유형, 클래스들 간의 관계 등을 그림으로 표현한 것
+ : public# : protected~ : default- : private변수의 데이터타입, 메소드의 반환타입을 표현할 때에는 이름 뒤에 :을 붙이고 자료형을 명시
👀 example
- 멤버필드
- num : int ➡ private int num;
+ name : String ➡ public String name;- 멤버메소드
+ display(n1 : int, n2 : int) : void / + display(int, int) : void
➡ public void display(int n1, int n2) { }

💡 밑줄 표시 ➡ static
public class Person {
private String name;
private int age;
public int dislpay(double data) {
}
}
---------------------------------------------
public class PersonEx {
public static void main (String[] args) {
Person p = new Person();
}
}