👀 한 눈에 알아보기
| 개념 | 설명 | 키워드 |
| 상속 | 부모 클래스의 기능을 자식 클래스가 물려받음 | extends |
| 추상 클래스 | 객체로 만들 수 없고, 자식 클래스에서 구현해야 함 | abstract |
| 인터페이스 | 메서드만 정의된 설계서, 다중 구현 가능 | interface , implements |
| 다형성 | 하나의 타입이 여러 형태의 객체를 참조할 수 있음 | 참조 타입 |
| 캡슐화 | 데이터 보호를 위한 접근 제어 | private , getter / setter |
1️⃣ 상속 (Inheritance)
- 기존 클래스의 기능을 재사용
- 새로운 기능을 추가하거나 변경
기본 구조)
public class A{
}
class B extends A{
}
ex)
// 부모 클래스 (Super class)
public class Animal {
void sound() {
System.out.println("동물이 소리를 냅니다.");
}
}
// 자식 클래스 (Sub class)
public class Dog extends Animal {
@Override // 부모 클래스의 sound() 메서드를 오버라이딩
void sound() {
System.out.println("멍멍!");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound(); // 출력: 멍멍!
}
}
- Dog는 Animal을 상속받아 sound() 메서드를 재정의(Override)한 것입니다.
2️⃣ 추상 클래스 (Abstract Class)
- 객체로 만들 수 없는 클래스
- 일부 메서드는 구현, 일부는 구현을 자식 클래스에게 맡기는 구조
- 추상 메서드는 하위 클래스에서 반드시 구현
ex)
abstract class Shape {
abstract void draw(); // 추상 메서드
void info() {
System.out.println("도형입니다.");
}
}
class Circle extends Shape {
@Override
void draw() { // 추상 메서드 하위 클래스에 구현
System.out.println("원을 그립니다.");
}
}
public class Main {
public static void main(String[] args) {
Shape shape = new Circle();
shape.draw(); // 출력: 원을 그립니다.
shape.info(); // 출력: 도형입니다.
}
}
3️⃣ 인터페이스 (Interface)
- 모든 메서드가 추상적
- 다중 상속이 가능
- 인터페이스는 기능(계약)을 약속하는 역할
- 클래스가 해당 인터페이스를 구현하면 인터페이스에 정의된 모든 메서드를 반드시 구현해야 합니다.
⚠ 코드 설계를 더 유연하고 유지보수하기 쉽게 만들어주는 핵심 개념
기본 구조)
interface A{
}
class B implements A{
}
ex)
interface Flyable {
void fly();
}
class Bird implements Flyable {
public void fly() {
System.out.println("새가 날아갑니다.");
}
}
public class Main {
public static void main(String[] args) {
Flyable bird = new Bird();
bird.fly(); // 출력: 새가 날아갑니다.
}
}
자바 클래스는 여러 인터페이스를 implements 키워드로 동시에 구현
ex) 다중 상속
public interface A { void showA(); }
public interface B { void showB(); }
public interface C { void showC(); }
public class MyClass implements A, B, C {
@Override public void showA() { System.out.println("A"); }
@Override public void showB() { System.out.println("B"); }
@Override public void showC() { System.out.println("C"); }
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.showA();
obj.showB();
obj.showC(); // 출력: A, B, C
}
}
4️⃣ 다형성 (Polymorphism)
- 같은 타입의 변수로 여러 객체를 참조
- 코드의 유연성과 확장성을 높여줌
ex)
// 인터페이스 정의
interface Animal {
void sound(); // 추상 메서드
}
// 인터페이스를 구현하는 클래스
class Dog implements Animal {
public void sound() {
System.out.println("멍멍"); // 추상 메서드 구현
}
}
// 인터페이스를 구현하는 다른 클래스
class Cat implements Animal {
public void sound() {
System.out.println("야옹"); // 추상 메서드 구현
}
}
// 인터페이스를 사용하는 코드
public class Main {
public static void main(String[] args) {
Animal dog = new Dog(); // Dog 클래스의 객체 생성
Animal cat = new Cat(); // Cat 클래스의 객체 생성
// 각 동물이 소리를 내도록 호출
dog.sound(); // 출력: 멍멍
cat.sound(); // 출력: 야옹
}
}
- Animal 타입으로 여러 하위 클래스를 다룸
5️⃣ 캡슐화 (Encapsulation)
- 필드를 private으로 감추고, getter/setter 메서드로 접근
public class Person {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Person p = new Person();
p.setName("홍길동");
System.out.println(p.getName());'Language > JAVA' 카테고리의 다른 글
| [JAVA] 컴파일(compile) / 빌드(build) (1) | 2025.01.15 |
|---|---|
| [JAVA 함수] 문자 바꾸기: split(), join() (0) | 2024.06.17 |
| [JAVA 기초 이론] 인터페이스: interface (0) | 2024.02.20 |
| [JAVA이론] StringBuffer / StringBuilder (0) | 2024.02.19 |
| 객체지향 프로그램(OOP)의 특성 (0) | 2023.05.26 |