[JDQ2] 자바JAVA_상속(2)Java/Daily Quiz2021. 9. 5. 18:00
목차
⚠️글 하단에 정답이 있습니다.
Q1.
Learner, Researcher 클래스를 만들고 각각 Person 클래스를 상속받게 한다. Student 클래스는 Learner 클래스를 상속받게 하고 Professor 클래스는 Researcher 클래스를 상속받게 한다. Person p가 선언되었을 때 다음 중 True인 것은?
- Person a = new Student(); 일때 a instanceof Person
- Person b = new Researcher(); 일때 b instance Professor
- Person c = new Professor(); 일때 c instance Researcher
- Person d = new Learner(); 일때 c instance Student
- Person e = new Researcher(); 일때 e instance Learner
Q2.
다음 코드의 메소드 실행 순서를 쓰고 실행 결과를 쓰세요.
class SuperObject {
public void paint() {
draw();
} // 1
public void draw() {
draw();
System.out.println("Super Object");
} // 2
}
class SubObject extends SuperObject {
public void paint() {
super.draw();
} // 3
public void draw() {
System.out.println("Sub Object");
} // 4
}
public class sample {
public static void main(String[] args) {
// TODO Auto-generated method stub
SuperObject b = new SubObject();
b.paint();
}
}
Q3.
다음 코드를 메소드 오버라이딩이 되게끔 고친 후 실행결과를 쓰세요.
class Shape {
protected String name;
public void paint() { draw(); }
public void draw() { System.out.println(name); }
}
public class Cirle extends Shape {
protected String name;
@Override
public void drow() {
name = "Circle";
super.name = "shape";
super.draw();
System.out.println("name");
}
public static void main(String [] args) {
Shape b = new Circle(); //업캐스팅
b.paint();
}
}
Q4.
다음 코드에서 틀린 부분이 있으면 고치세요.
class A {
abstract public void f();
}
class B extends A { ... }
정답
A1.
1, 3
A2.
순서 : 3 -> 2 -> 4
<실행 결과>
Sub Object
Super Object
A3.
class Shape {
protected String name;
public void paint() { draw(); }
public void draw() { System.out.println(name); }
}
public class Cirle extends Shape {
protected String name;
@Override
public void draw() //drow -> draw 부모 클래스의 메소드 이름과 같아야 함
{
name = "Circle";
super.name = "shape";
super.draw();
System.out.println(name);
}
public static void main(String [] args) {
Shape b = new Circle(); //업캐스팅
b.paint();
}
}
<실행 결과>
shape
Circle
A4.
abstract class A {
abstract public void f();
}
class B extends A { ... }
//추상 메소드를 가지고 있으면 반드시 추상 클래스로 선언되어야 한다.
728x90
반응형
LIST
'Java > Daily Quiz' 카테고리의 다른 글
[JDQ4] 자바JAVA_모듈과 패키지(2) (0) | 2021.09.12 |
---|---|
[JDQ3] 자바JAVA_모듈과 패키지 (0) | 2021.09.11 |
[JDQ1] 자바JAVA_상속 (0) | 2021.09.04 |
@kdj :: Childev'note
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!