JAVA 상속: 문제풀이(2)

keymu·2024년 9월 25일
0

if문은 false이므로 패스
print("2 ") 실행
다음 if문도 false이므로 pass
인 줄 알았으나
b2 = true로 바꾸고, b1도 true이므로 실행이 된다.
따라서 print된 구문은 2 3

답: 2 3


Vert: 자식
Blip: 부모코드 분석 및 가능한 메서드 선택

  1. public int blipvert(int x) { return 0; }
    오버라이딩 가능

  2. private int blipvert(int x) { return 0; }
    불가. 오버라이딩 private으로 변경은 불가

  3. private int blipvert(long x) { return 0; }
    오버로딩 가능. 변수 바뀜.

  4. protected long blipvert(int x) { return 0; }
    불가. 변수가 같기 때문에 오버로딩으로 봐야하는데, 반환값의 타입이 다르므로.

  5. protected int blipvert(long x) { return 0; }
    오버로딩 가능. 변수 다르므로 오버로딩으로 봐야하는데 접근제한자가

  6. protected long blipvert(long x) { return 0; }
    오버로딩 가능.

  7. protected long blipvert(int x, int y) { return 0; }
    오버로딩 가능.

컴파일되는 5개의 메서드는 다음과 같습니다.

public int blipvert(int x) { return 0; } //A
protected int blipvert(long x) { return 0; } //C
protected int blipvert(long x) { return 0; } //E
protected long blipvert(long x) { return 0; } //F
protected long blipvert(int x, int y) { return 0; } //G

잘못된 형 변환 (Downcasting): animal 변수는 Animal 타입이지만, 실제로는 Dog 객체를 참조하고 있다.

Cat cat = (Cat)animal; 

코드에서 animal을 Cat 타입으로 강제 형 변환하려고 시도하는데, 이는 Dog 객체를 Cat 객체로 바꿀 수 없기 때문에 ClassCastException 예외가 발생한다.

만약 형 변환이 정상적으로 이루어졌다면, cat.noise()를 호출했을 때 Dog 클래스의 noise() 메서드가 실행되어 "bark"가 출력되었을 것이나, 오류가 발생했기 때문에 print 되지 않는다.

답: E


private < default < protected < public

답: A, B, E, F


The output is mm:
DeluxeThingy 객체의 m 필드는 Meter 타입
두 번의 메서드 호출 모두 Meter 클래스의 go() 메서드를 실행하여 "m"을 출력
따라서 최종 출력은 "mm"

DeluxeThingy has-a Component:
DeluxeThingy는 Thingy를 상속받기 때문에 Thingy의 속성과 메소드를 가지고 있다.
Thingy는 Meter를 포함하고 있기 때문에, 간접적으로 DeluxeThingy도 Meter를 가지고 있다.

답:
The output is mm.
DeluxeThingy has-a Component.

profile
Junior Backend Developer

0개의 댓글