개발/JAVA

Java 객체지향 프로그래밍 - Point 클래스와 상속 개념 배우기

예니03 2025. 2. 6. 10:04
반응형

Java에서 객체지향 프로그래밍(OOP)의 핵심 개념인 클래스, 생성자, 메서드 오버로딩, 상속을 이해하기 위해 Point 클래스를 만들어보고, 이를 확장한 ColorPoint 클래스를 구현해보겠습니다. Java를 처음 배우는 분들도 이해하기 쉽게 하나씩 설명해드릴게요! 😊

1. 기본적인 Point 클래스 구현 (v1)

먼저, 기본적인 Point 클래스를 만들어 보겠습니다. 이 클래스는 x와 y 좌표를 가지며, 점을 출력하는 기능을 합니다.

🔹 Point 클래스 (v1)

package com.javaex.oop.point.v1;

public class Point {
    // 필드 (멤버 변수)
    private int x;
    private int y;

    // 기본 생성자
    public Point() {
    }

    // Getter & Setter
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }

    // 점을 출력하는 메서드
    public void draw() {
        System.out.printf("점[x=%d, y=%d]을 그렸습니다.%n", x, y);
    }
}

🔹 PointApp 클래스 (실행)

package com.javaex.oop.point.v1;

public class PointApp {
    public static void main(String[] args) {
        Point one = new Point();
        one.setX(5);
        one.setY(5);
        
        Point two = new Point();
        two.setX(10);
        two.setY(23);
        
        one.draw();
        two.draw();
    }
}

실행 결과:

점[x=5, y=5]을 그렸습니다.
점[x=10, y=23]을 그렸습니다.

 

2. 생성자 추가 (v2)

기본 생성자뿐만 아니라, x, y 값을 한 번에 설정할 수 있는 생성자를 추가하면 더 편리합니다.

package com.javaex.oop.point.v2;

public class Point {
    private int x;
    private int y;

    // 기본 생성자
    public Point() {
    }
    
    // 모든 필드를 초기화하는 생성자
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    
    public void draw() {
        System.out.printf("점[x=%d, y=%d]을 그렸습니다.%n", x, y);
    }
}

실행 시 new Point(5, 5)와 같이 간결하게 객체를 생성할 수 있습니다!

 

3. 메서드 오버로딩 추가 (v3)

메서드 오버로딩(Method Overloading)이란 같은 이름의 메서드를 여러 개 만들되, 매개변수의 개수나 타입을 다르게 설정하는 기능입니다.

🔹 draw() 메서드 오버로딩

public void draw(boolean bDraw) {
    String message = String.format("점[x=%d, y=%d]을", x, y);
    message += bDraw ? " 그렸습니다." : " 지웠습니다.";
    System.out.println(message);
}

실행 예시:

one.draw(true);  // 점을 그렸습니다.
one.draw(false); // 점을 지웠습니다.

 

4. 상속 (v4) - ColorPoint 클래스 추가

이제, Point 클래스를 상속하여 색상을 추가한 ColorPoint 클래스를 만들어 봅니다.

🔹 ColorPoint 클래스

package com.javaex.oop.point.v4;

public class ColorPoint extends Point {
    private String color;
    
    // 생성자
    public ColorPoint(String color) {
        super(0, 0); // 부모(Point)의 기본 생성자 호출
        this.color = color;
    }
    
    public ColorPoint(int x, int y, String color) {
        super(x, y); // 부모의 생성자 호출
        this.color = color;
    }
    
    @Override
    public void draw() {
        System.out.printf("색깔점[x=%d, y=%d, color=%s]을 그렸습니다.%n", x, y, color);
    }
}

실행 예시:

ColorPoint cp = new ColorPoint("red");
cp.draw();

출력 결과:

색깔점[x=0, y=0, color=red]을 그렸습니다.

 

5. 전체 실행 코드 (PointApp)

package com.javaex.oop.point.v4;

public class PointApp {
    public static void main(String[] args) {
        Point one = new Point(5, 5);
        Point two = new Point(10, 23);
        
        one.draw();
        two.draw();
        
        one.draw(true);
        one.draw(false);
        
        ColorPoint cp = new ColorPoint("red");
        cp.draw();
        
        ColorPoint cp2 = new ColorPoint(10, 10, "blue");
        cp2.draw();
    }
}

실행 결과:

점[x=5, y=5]을 그렸습니다.
점[x=10, y=23]을 그렸습니다.
점[x=5, y=5]을 그렸습니다.
점[x=5, y=5]을 지웠습니다.
색깔점[x=0, y=0, color=red]을 그렸습니다.
색깔점[x=10, y=10, color=blue]을 그렸습니다.

 

💡 정리

생성자를 활용해 객체를 초기화하면 코드가 더 간결해진다. ✅ 메서드 오버로딩을 사용하면 같은 기능을 다양한 방식으로 활용 가능하다. ✅ 상속을 이용하면 기존 클래스를 확장하여 새로운 기능을 추가할 수 있다.🚀

반응형