Chapter 1: 서브 클래스 작성
Java에서의 접근 제어
Java에서 접근 제어자는 클래스 및 멤버 변수의 접근 범위를 제어합니다.
- private: 같은 클래스 내부에서만 접근 가능
- default (패키지-프라이빗): 같은 패키지 내에서 접근 가능
- protected: 같은 패키지 및 하위 클래스에서 접근 가능
- public: 어디서나 접근 가능
클래스 확장
클래스를 확장하면 기존 클래스의 속성과 메서드를 재사용할 수 있습니다.
class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
슈퍼 클래스 멤버에 액세스
서브 클래스에서 super 키워드를 사용하여 슈퍼 클래스의 메서드를 호출할 수 있습니다.
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void eat() {
super.eat(); // 슈퍼 클래스 메소드 호출
System.out.println("Dog is eating");
}
}
슈퍼 클래스 생성자 호출
서브 클래스에서 슈퍼 클래스의 생성자를 호출하려면 super()를 사용합니다.
class Animal {
Animal(String name) {
System.out.println("Animal: " + name);
}
}
class Dog extends Animal {
Dog() {
super("Dog");
System.out.println("Dog created");
}
}
Chapter 2: 메소드 구현
가상 메소드(Virtual Method)
Java에서는 모든 인스턴스 메소드가 기본적으로 가상 메소드입니다.
class Animal {
void makeSound() {
System.out.println("Animal makes sound");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
메소드 오버라이딩
서브 클래스에서 슈퍼 클래스의 메서드를 재정의할 수 있습니다.
class Parent {
void show() {
System.out.println("Parent method");
}
}
class Child extends Parent {
@Override
void show() {
System.out.println("Child method");
}
}
final 메소드
final 키워드를 사용하면 메소드를 오버라이드할 수 없습니다.
class Parent {
final void show() {
System.out.println("This method cannot be overridden");
}
}
final 클래스
final 키워드를 사용하면 해당 클래스를 확장할 수 없습니다.
final class Parent {
}
// class Child extends Parent {} // 오류 발생
Chapter 3: 인터페이스
인터페이스 선언
interface Animal {
void makeSound();
}
다중 인터페이스 구현
Java에서는 클래스가 여러 개의 인터페이스를 구현할 수 있습니다.
interface Animal {
void eat();
}
interface Mammal {
void sleep();
}
class Dog implements Animal, Mammal {
public void eat() {
System.out.println("Dog eats");
}
public void sleep() {
System.out.println("Dog sleeps");
}
}
인터페이스 메소드 구현
interface Vehicle {
void start();
}
class Car implements Vehicle {
public void start() {
System.out.println("Car starting...");
}
}
인터페이스의 default 메소드
Java 8부터 인터페이스에서 default 메서드를 정의할 수 있습니다.
interface Vehicle {
default void start() {
System.out.println("Vehicle is starting...");
}
}
class Car implements Vehicle {
public void start() {
System.out.println("Car starting...");
}
}
인터페이스에서 static 메소드 구현
인터페이스에서 정적 메서드를 선언할 수도 있습니다.
interface Utility {
static void printMessage() {
System.out.println("Utility Message");
}
}
Utility.printMessage(); // 정적 메소드 호출
Chapter 4: 추상 클래스
추상 클래스 선언
추상 클래스는 인스턴스를 직접 생성할 수 없으며, 서브 클래스에서 구현해야 할 추상 메서드를 가질 수 있습니다.
abstract class Animal {
abstract void makeSound();
}
클래스 계층 구조에서 추상 클래스 사용
추상 클래스는 특정 속성을 공통적으로 제공하며, 서브 클래스에서 동작을 정의하도록 합니다.
abstract class Animal {
abstract void makeSound();
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog barks");
}
}
클래스 계층 구조에서 추상 클래스 사용 (2)
abstract class Vehicle {
abstract void start();
}
class Car extends Vehicle {
void start() {
System.out.println("Car is starting");
}
}
추상 클래스와 인터페이스
추상 클래스와 인터페이스의 차이점은 다음과 같습니다.
- 추상 클래스: 상태(필드)와 행동(메소드)를 가질 수 있음
- 인터페이스: 오직 행동(메소드)만 정의 가능 (Java 8 이후 default/static 메소드 허용)
추상 메소드 구현
abstract class Animal {
abstract void makeSound();
}
class Cat extends Animal {
void makeSound() {
System.out.println("Cat meows");
}
}