| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
- @
- 어노테이션
- spring
- PointCut
- application.properties
- jointpoint
- 생성자주입
- 바인딩변수
- 유효성검사
- @Valid
- SpringBoot
- @ResponseBody
- @RequestMapping
- springjdbc
- after-throwing
- springmvc
- MVC
- Java
- c:if
- 서비스레이어
- 비즈니스레이어
- frontController
- produces
- .xml
- 스프링
- AOP
- Model
- @RequestParam
- 의존주입
- gradle
- Today
- Total
메모장
[과제] 멘토씨리즈 문제풀이 3 본문
상속
1.다음중 상속과 관련된 단어를 모두 고르세요
1) super 2) super() 3) this 4) extends
2. 다음 코드를 실행 했을때 콘솔창에 출력되는 결과는 ?
package section02;
class Person{
void printHello() {
System.out.println("안녕하세요.");
}
}
class Student extends Person{
}
public class PRACTICE_03_05 {
public static void main(String[] args) {
Person p = new Person();
p.printHello();
Student s = new Student();
s.printHello();
}
}
정답 : 안녕하세요 . 안녕하세요 . 두번 출력된다. 학생이라는 클래스가 사람이라는 클래스를 상속받고 있기 때문에 학생 클래스가 자식 클래스가 되는데 자식 클래스는 부모 클래스의 것을 가져다가 쓸 수 있다.
3. <PRACTICE_03_05> 위의 코드에서 class Student가 Person 의 printHello()를 다음과 같이 오버라이드 했을때 콘솔창에 출력되는 결과는 무엇입니까 ?
class Student extends Person{
@Override
void printHello() {
System.out.println("안녕하세요, 저는 자바를 공부하는 학생입니다.");
}
}
정답 : " 안녕하세요, 저는 자바를 공부하는 학생입니다 " 라고 출력이 나온다 . 오버라이딩을 하게 되면 부모 클래스의 메서드를 내가 재정의 해서 자식 클래스 메서드로 재정의 된 내용이 출력된다.
4. 다음 코드는 컴파일 에러가 발생합니다. 그 이유는 무엇이고 어떻게 해결해야 할까요 ?
<ClassA.java>
package section02;
public class ClassA{
private int a;
private ClassA(int a) {
this.a = a;
}
public void methodA() {
System.out.println("ClassA클래스의 methodA() 메서드입니다.");
System.out.println("필드 a의 값은 " + a + "입니다.");
}
}
<ClassB.java>
package section02;
public class ClassB {
public static void main(String[] args) {
ClassA ca = new ClassA(3);
ca.methodA();
}
}
정답 : private 를 붙여주면 클래스 외부에서 접근이 불가능해진다. 따라서 객체인 클래스로 생성 할수 없다.
private 를 지워주기 .
5. 다음 두 개의 클래스는 상속 관계에 있으나 , 다른 패키지에 속해 있습니다. child 클래스 에서 Parent 클래스의 parentMethod 메서드를 호출하기 위해 어떤 코드를 작성해야 할까
<Parent.java>
package section10.access1;
public class Parent{
protected void parentMethod() {
System.out.println("parentMethod is called.");
}
}
<Child.java>
package section10.access2;
import section10.access1.*;
public class child{
void accessParentMethod() {
?
}
}
정답: protected가 붙었을때 같은 패키지에서 접근을 허용하지만 다른 패키지 라도 상속 관계가 된다면 가능해진다. child가 Parent 를 상속받고( child extends Parent) 물음표 안에서는 부모클래스의 메서드를 super. 를 붙여서 가져올수 있다.
다형성과 타입변환
- 다음코드는 컴파일 에러가 발생합니다. 컴파일 에러가 발생하는 곳을 모두 찾아 수정해 보세요
package section11;
class Car {}
class Bus extends Car {}
class SchoolBus extends Bus{}
class OpenCar extends Car{}
class SportsCar extends OpenCar{}
public class PRACTICE_11_01 {
public static void main(String[] args) {
Car c1 = new SchoolBus();
Bus b1 = new Bus();
SchoolBus sb = new Car(); // 수정 : Car sb = new SchoolBus();
Car c2 = new OpenCar();
OpenCar oc = new SportsCar();
Bus b3 = new OpenCar(); // 수정 : Car b3 = new OpenCar();
Bus b4 = new SportsCar(); // 수정 : OpenCar b4 = new SportsCar();
}
}
정답 : OpenCar 클래스와 SportsCar 클래스는 Bus 클래스와 같은 상위 계층의 클래스 Car 와 상속 관계는 맞지만, Bus 클래스와 직접적인 상속관계가 아니므로 자동 타입 변환을 할 수 없다 ! 자동 타입 변환은 반드시 상속관계에 있는 자식 클래스의 객체를 부모타입으로 변환할때 적용할 수 있는데 지금 부모클래스의 객체를 자식타입에 넣으려고 해서 컴파일 에러가 발생하였다.
2. 다음 설명에 해당하는 용어는 ?
부모 클래스에게 상속받은 메서드를 재정의하여 자식 클래스용 메서드를 구현하고 자식 객체를 통해 메서드를
호출 할때는 부모의 메서드가 아니라 자식의 메서드가 호출된다.
정답 : 오버라이딩
3. 다음과 같은 결과가 나오도록 아래 클래스를 구현해주세요
- class Speaker
- class RedSpeaker
- class BlueSpeaker
package section11;
class Person{
Speaker speaker;
Person(Speaker speaker){
this.speaker = speaker;
}
void turnOn() {
System.out.println(speaker.getName() + "이 켜졌습니다.");
}
}
public class PRACTICE_11_03 {
public static void main(String[] args) {
Speaker s1 = new RedSpeaker();
Person p1 = new Person(s1);
p1.turnOn();
Speaker s2 = new RedSpeaker();
Person p2 = new Person(s2);
p2.turnOn();
} 실행결과
} 빨간 스피커가 켜졌습니다.
파란 스피커가 켜졌습니다.
package section11;
class Person{
private Speaker speaker;
Person(Speaker speaker){
this.speaker = speaker;
}
void turnOn() {
System.out.println(speaker.getName() + "이 켜졌습니다.");
// 스피커에 get으로 받아오는것을 보고 private을 추측
}
}
class Speaker { // Speaker 와 Person은 상속 관계가 아님. // 상속은 연관되었을때 !
// Speaker의 전원이 켜진다고 Person 이 켜지는게 아니기 때문에
private String name;
Speaker(String name){
this.name = name;
}
void turnOn() {
System.out.println(this.name + "이 켜졌습니다.");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class RedSpeaker extends Speaker{
RedSpeaker(){
this("빨강스피커");
}
RedSpeaker(String name){
super("빨강스피커");
}
}
class BlueSpeaker extends Speaker{
BlueSpeaker(){
this("파란스피커");
}
BlueSpeaker(String name){
super("파란스피커");
}
}
public class PRACTICE_11_03 {
public static void main(String[] args) {
Speaker s1 = new RedSpeaker("빨간스피커");
Person p1 = new Person(s1);
p1.turnOn();
Speaker s2 = new BlueSpeaker("파란스피커");
Person p2 = new Person(s2);
p2.turnOn();
}
}
'JAVA > [멘토씨리즈]' 카테고리의 다른 글
| [멘토씨리즈] 예외처리 (0) | 2024.04.09 |
|---|---|
| [멘토 씨리즈] 다형성과 타입변환 (0) | 2024.04.09 |
| [멘토 씨리즈] 메서드 (1) | 2024.04.09 |
| [과제] 멘토씨리즈 문제풀이 2 (0) | 2024.04.09 |
| [멘토 씨리즈] JAVA 다형성과 타입변환 (0) | 2024.04.09 |