7-17~20 제어자(modifier), static, final, abstract

oyeon·2020년 12월 27일
0

(완)객체지향 개념

목록 보기
23/37

제어자(modifier)

  • 클래스와 클래스의 멤버(멤버 변수, 메서드)에 부가적인 의미 부여
  • 접근 제어자 : public, protected, (default), private
  • 그 외 : static, final, abstract, native, transient, synchronized, volatile, strictfp
  • 하나의 대상에 여러 제어자를 같이 사용 가능(접근 제어자는 하나만)
public class ModifierTest{
    public static final int WIDTH = 200;
    
    public static void main(String[] args){
    	System.out.println("WIDTH="+WIDTH);
    }
}

static - 클래스의, 공통적인

class StaticTest{
    static int width = 200;	// 클래스 변수(static 변수)
    static int height = 120;	// 클래스 변수(static 변수)
    
    static {			// 클래스 초기화 블럭
    	// static 변수의 복잡한 초기화 수행
    }
    
    static int max(int a, int b){	// 클래스 메서드(static 메서드)
    	return a > b ? a : b;
    }
}

final - 마지막의, 변경될 수 없는

final class FinalTest{			// 조상이 될 수 없는 클래스(상속 계층도의 마지막)
    final int MAX_SIZE = 10;		// 값을 변경할 수 없는 멤버변수(상수)
    
    final void getMaxSize(){		// 오버라이딩 할 수 없는 메서드(변경 불가)
    	final int LV = MAX_SIZE;	// 값을 변경할 수 없는 지역변수(상수)
        return MAX_SIZE;
    }
}

abstract - 추상의, 미완성의

abstract class AbstractTest{	// 추상 클래스(추상 메서드를 포함한 클래스)
    abstract void move();	// 추상 메서드(구현부가 없는 메서드)
}

// 에러!! 추상 클래스의 인스턴스 생성불가(∵미완성 설계도라서 제품 생산 불가능)
// -> 추상 클래스를 상속받아서 완전한 클래스를 만든 후 객체 생성 가능
AbstractTest a = new AbstractTest();
profile
Enjoy to study

0개의 댓글