상속 문제

2022. 1. 30. 17:14할 수 있다! 백준 &문제 풀기

인터페이스 AdderInterface의 코드는 아래와 같다. 

interface AdderInterface {
	int add(int x, int y);
	int add(int n);
}

AdderInterface를 상속바은 클래스를 작성하여 다음 메인을 실행할 때 아래 결과와 같이 출력되도록 해라 (첫번째 15 두번째 55)

	public static void main(String[] args) {
		
		Ch5Practice2MyAdder adder=new Ch5Practice2MyAdder();
		System.out.println(adder.add(5,10));
		System.out.println(adder.add(10));
	}

 


implement 상속 개념 이용

클래스를 완성할 때 public 타입으로 선언하여야 한다.

 


interface AdderInterface {
	int add(int x, int y);
	int add(int n);
}
public class Ch5Practice2MyAdder implements AdderInterface {
	
	
	public int add(int x, int y) {
		return x+y;
	}
	
	public int add(int n) {
		int sum=0;
		for(int i=1; i<n; i++)
			sum=sum+i;
		return sum;
	}
	
		
	
	public static void main(String[] args) {
		
		Ch5Practice2MyAdder adder=new Ch5Practice2MyAdder();
		System.out.println(adder.add(5,10));
		System.out.println(adder.add(10));
	}

}

(츨차: 명품 자바 에디션)