추상 메서드: 선언부는 있지만 구현부는 없는~
추상 클래스: 추상 메서드를 하나라도 갖고 있으면 추상 클래스로 선언해야함
public abstract class Animal {
abstract void cry();
}
Animal Jimseung = new Animal();
→ Error! Animal 타입은 객체를 만들 수 없다.
👉 추상 메서드는 하위 클래스에게 메서드의 구현(오버라이딩)을 강제함.
static 블록
: 클래스가 static 영역에 배치될 때 실행됨!!
public class Animal {
**static** {
System.out.println("동물 클래스 ready");
}
}
➕ static 변수의 초기화 시점
[클래스 로더의 클래스 로드 과정]
final 키워드
final 키워드가 붙은 클래스는 상속을 허락하지 않는다!
→ 하위 클래스를 만들 수 없다
final 키워드가 붙은 변수는 변경을 허락하지 않는다!
→ 한번 초기화된 변수는 값을 변경할 수 없다
→ 초기화는 선언 시/static블록 내부 에서 가능하다
• static으로 선언한 변수 : 객체 생성 시 초기화?
-> 선언 시에/static 블록 내부에서 가능
final 키워드가 붙은 메서드는 오버라이딩을 허락하지 않는다!
만들어진 객체가 특정 클래스의 인스턴스인지 물어보는 연산자
→ true 나 false 반환
class Animal {
}
class Bird extends Animal {
}
public class Driver {
public static void main(String[] args) {
Animal animal1 = new Animal();
**Bird** bird1 = new Bird();
System.out.println(bird1 instanceof Animal); // true
System.out.println(bird1 instanceof Bird); // true
}
}
public class Driver {
public static void main(String[] args) {
Animal animal1 = new Animal();
************Aniaml************ bird1 = new Bird();
System.out.println(bird1 instanceof Animal); // true
System.out.println(bird1 instanceof Bird); // true
}
}
→ 객체 참조 변수의 타입이 아닌 실제 객체의 타입에 의해 처리하기 때문!
🧐 instanceof 연산자는 상속관계 뿐만 아니라 인터페이스의 구현 관계에서도 동일하게 작용
interface CanFly {
}
class Parrot implements CanFly {
}
public class Driver {
public static void main(String[] args) {
CanFly parrot1 = new Parrot();
System.out.println(parrot1 instanceof CanFly); // true
System.out.println(parrot1 instanceof Parrot); // true
}
}
도서: 스프링 입문을 위한 자바 객체지향의 원리와 이해 (김종민, 위키북스)