사전교육 3주차
개요
3주차에는 [클래스, 상속, 인터페이스]에 대한 내용이다. 개념에 대해 내가 이해한 내용을 정리하고 끝에는 과제 풀이에 대해 작성해보려고 한다.
개념 정리
-
- 클래스
- 클래스는 객체를 생성하기 위한 설계도이다.
객체는 물리적으로 존재하는 것을 의미하고 객체는 속성과 행위로 구분지을 수 있다.
속성은 흔히 말하는 필드를 의미하고 (객체의 데이터를 저장하는 역할) 행위는 메서드를 의미한다.클래스를 선언하는 방식에는 4개의 step이 있다.
step1. class 선언
step2. 객체가 지녀야 할 속성(필드) 정의
step3. 객체의 생성 방식 정리(생성자)
step4. 객체가 지녀야 할 행위(메서드) 정의Car --- + company: String + model: String + gear: char --- + Car() --- + gasPedal(): double + hom(): voidthis와 this() 차이
- this는 현재 클래스의 자기 자신을 가리키는 것 - this()는 중복된 코드를 줄일 수 있고 같은 클래스의 다른 생성자를 호출할 때 사용함 ** java code ** public class Car { String model; int year; // 생성자 1 public Car(){ this("Unknown", 2000); // this()로 다른 생성자 호출 } // 생성자 2 public Car(String model, int year){ this.model = model; this.year = year; } } public class Main(){ public statics vodi main(String[] args){ Car car1 = new Car(); // 기본 생성자 호출 -> this()로 다른 생성자 호출 Car car2 = new Car("Tesla", 2025); // 생성자 2 호출 } } -
- 상속
- 부모 클래스의 필드와 메서드를 자식 클래스에게 물려주는 것
상속을 사용하면 적은 양의 코드로 새로운 클래스를 작성할 수 있고 공통적인 코드를 관리하여 코드의 추가와 관리가 용이해짐
클래스 간 상속과 포함 관계
- 상속 관계 : is - a ("~은 ~이다") - 포함 관계 : has - a ("~은 ~을 가지고 있다")super와 super()의 차이
- super는 부모 클래스의 필드나 메서드에 접근할 때 사용 자식 클래스에서 부모 클래스의 기능을 확장하거나 오버라이드할 때 유용함 ** java code ** // 부모 클래스 Car String model; // 자동차 모델 String color; // 자동차 색상 double price; // 자동차 가격 // 자식 클래스 SportsCar String model = "Ferrari"; // 자동차 모델 String color = "red": // 자동차 색상 double price = 300000000; // 자동차 가격 // 자식 클래스의 메서드 public void setCarInfo(String model, String color, double price){ super.model = model; // model은 부모 필드에 set super.color = color; // color은 부모 필드에 set this.price = price; // price는 자식 필드에 set } - super()는 부모 클래스의 생성자를 호출할 떄 사용 자식 클래스의 생성자에서 부모 클래스의 생성자를 먼저 실행해야할 때 사용함 항상 생성자 내부에서 첫 줄에 위치해야 함 ** java code ** // 부모 클래스 Car public Car(String model, String color, double price){ this.model = model; this.color = color; this.price = price; } // 자식 클래스 class SportsCar extends Car{ public SportsCar(String model, String color, double price, String engine){ super(model, color, price); // 부모 클래스 Car의 생성자를 호출하는 역할 this.engine = engine; } }추상 클래스 추상 메서드
- 추상 클래스 : 미완성된 설계도 abstract 키워드를 사용하여 추상 클래스를 선언할 수 있다. 여러 개의 자식 클래스들에서 공통적인 필드나 메서드를 추출해서 만들 수 있다. - 추상 메서드 : 미완성된 메서드 abstract 키워드를 사용하여 추상 메서드를 선언할 수 있다. 추상 메서드는 일반적인 메서드와는 다르게 블록{}이 없다. -
- 인터페이스
- 인터페이스는 두 객체를 이어주는 다리 역할을 해준다.
과제 내용

과제 풀이
1. Calculator 클래스 정의
public class Calculator(){
private AbstractOperation operation; // 추상 클래스 타입으로 참조
// 추상 클래스 타입을 받아 생성자 생성
public Calculator(AbstractOperation operation){
this.operation = operation;
}
public void setOperation(AbstractOperation operation){
this.operation = operation;
}
public double calculate(int firstNumber, int secondNumber){
double answer = 0;
answer = operation(firstNumber, secondNumber);
return answer;
}
}
2. AbstractOperation 추상 클래스 정의
public abstract AbstractOpertion(){
public abstract double operate(int firstNumber, int secondNumber);
}
3. AddOperation 클래스 정의
public AddOperation extends AbstractOperation(){
@Override
public double operate(int firstNumber, int secondNumber){
return firstNumber + secondNumber;
}
}
4. SubstractOperation 클래스 정의
public SubstractOperation extends AbstractOperaion(){
@Override
public double operate(int firstNumber, int secondNumber){
return firstNumber - secondNumber;
}
}
5. MultiplyOperation 클래스 정의
public MultiplyOperation extends AbstractOperaion(){
@Override
public double operate(int firstNumber, int secondNumber){
return firstNumber * secondNumber;
}
}
6. DivideOperation 클래스 정의
public SubstractOperation extends AbstractOperaion(){
@Override
public double operate(int firstNumber, int secondNumber){
return firstNumber / secondNumber;
}
}
7. Main 클래스 정의
public class Main {
public static void main(String[] args){
// 객체 선언
Calculator calculator = new Calculator(new AddOperation());
System.out.println(calculator.calculate(10, 20));
calculator.setOperation(new MultiplyOperation());
System.out.println(calculator.calculate(10, 20));
}
}
실행 결과
