다음 클래스에 대해 물음에 답하라.
class A {
private int a;
public void set(int a) { this.a = a; }
}
class B extends A {
protected int b, c;
}
class C extends B {
public int d, e;
}
A objA = new objA();
에 의해 생성되는 객체 objA의 멤버들을 모두 나열하라.private int a;
public void set(int a) {this.a=a;}
B objB = new objB();
에 의해 생성되는 객체 objB의 멤버들을 모두 나열하라.private int a;
public void set(int a) {this.a=a;}
protected int b, c;
C objC = new objC();
에 의해 생성되는 객체 objC의 멤버들을 모두 나열하라.private int a;
public void set(int a) {this.a=a;}
protected int b, c;
public int d, e;
class D extends C {
public void f() {
a = 1; // (1)
set(10); // (2)
b = 20; // (3)
d = 30; // (4)
}
}
(1) a는 private 멤버이기 때문에 접근할 수 없다.
자바의 모든 클래스가 반드시 상속받게 되어 있는 클래스는?
상속을 이용하여 다음 클래스들을 간결한 구조로 재작성하라.
class SharpPencil { // 샤프펜슬
private int width; // 펜의 굵기
private int amount; // 남은 량
public int getAmount() { return amount; }
public void setAmount (int amount) { this.amount = amount; }
}
class Ballpen { // 볼펜
private int amount; // 남은 량
private String color; // 볼펜의 색
public int getAmount() { return amount; }
public void setAmount (int amount) { this.amount = amount; }
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
}
class FountainPen { // 만년필
private int amount; // 남은 량
private String color; // 볼펜의 색
public int getAmount() { return amount; }
public void setAmount (int amount) { this.amount = amount; }
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
public void refill (int n) { setAmount(n); }
class Pen {
private int amount; // 남은 량
public int getAmount() { return amount; }
public void setAmount (int amount) { this.amount = amount; }
}
class SharpPencil extends Pen { // 샤프펜슬
private int width; // 펜의 굵기
}
class Ballpen extends Pen { // 볼펜
private String color; // 볼펜의 색
public String getColor() { return color; }
public void setColor(String color) { this.color = color; }
}
class FountainPen extends Ballpen{ // 만년필
public void refill (int n) { setAmount(n); }
다음 중 설명에 적절한 단어를 기입하라.
자바에서 상속받는 클래스를 ________라고 부르며, _____ 키워드를 이용하여 상속을 선언한다. 상속받은 클래스에서 상속해준 클래스의 멤버를 접근할 때 ______ 키워드를 이용한다. 한편, 객체가 어떤 클래스의 타입인지 알아내기 위해서는 ________ 연산자를 이용하며, 인터페이스는 클래스와 달리 ________ 키워드를 이용하여 선언한다.
서브 클래스 / extends / super / instanceof / inteface
상속에 관련된 접근 지정자에 대한 설명이다. 틀린 것은?
다음 빈칸에 적절한 한 줄의 코드를 삽입하라.
class TV {
private int size;
public TV(int n) { size = n; }
}
class ColorTV extends TV {
private int colors;
public ColorTV(int colors, int size) {
___________
this.colors = colors;
}
}
super(size);
상속에 있어 생성자에 대해 묻는 문제이다. 실행될 때 출력되는 내용은 무엇인가?
class A {
public A() { System.out.println("A"); }
public A(int x) { System.out.println("A: " + x); }
}
class B extends A {
public B() { super(100); }
public B(int x) { System.out.println("B: " + x); }
}
public class Example {
public static void main(String[] args) {
B b = new B(11);
}
}
A
B: 11
다음 코드에서 생성자로 인한 오류를 찾아내어 이유를 설명하고 오류를 수정하라.
class A {
private int a;
protected A(int i) { a = i; }
}
class B extends A {
private int b;
public B() { b = 0; }
}
public A() {}
을 만들거나 public B() {super(1); b=0;}
으로 수정한다.
다음 추상 클래스의 선언이나 사용이 잘못된 것을 있는 대로 가려내고 오류를 지적하라.
abstract class A {
void f();
}
abstract viud f();
로 수정한다.
abstract class A {
void f() {
System.out.println("~");
}
}
abstract class B {
abstract void f();
}
class C extends B {}
abstract class C extends B{}
로 수정한다.
abstract class B {
abstract int f();
}
class C extends B {
void f() {
System.out.println("~");
}
}
int f() {System.out.println("~"); return 0;}
으로 수정한다.
추상 클래스를 구현하는 문제이다. 실행 결과와 같이 출력되도록 클래스 B를 완성하라.
abstract class OddDetector {
protected int n;
public OddDetector (int n) {
this.n = n;
}
public abstract boolean isOdd(); // 홀수이면 true 리턴
}
public class B extends OddDetector {
public B(int n) {
super(n);
}
public static void main(String[] args) {
B b = new B(10);
System.out.println(b.isOdd());
}
}
false
abstract class OddDetector {
protected int n;
public OddDetector (int n) {
this.n = n;
}
public abstract boolean isOdd(); // 홀수이면 true 리턴
}
public class B extends OddDetector {
public B(int n) {
super(n);
}
public boolean isOdd() {
if(n%2==1)
return true;
else return false;
}
public static void main(String[] args) {
B b = new B(10);
System.out.println(b.isOdd());
}
}
다음 상속 관계의 클래스들이 있다.
class A {
int i;
}
class B extends A {
int j;
}
class C extends B {
int k;
}
class D extends B {
int m;
}
A x = new D();
System.out.println(x instanceof B);
System.out.println(x instanceof C);
true
false
System.out.println(new D() instanceof Object);
System.out.println("Java" instanceof Object);
true
true
동적 바인딩에 관한 문제이다. 다음 코드가 있을 때 질문에 답하라.
class Shape {
public void draw() { System.out.println("Shape"); }
}
class Circle extends Shape {
public void paint() {
__________________
}
public void draw() { System.out.println("Circle"); }
}
Shape s = new Circle(); s.draw()
가 호출되면 출력되는 내용은?s.paint()
가 호출되면 "Circle"이 출력되도록 빈 칸에 적절한 코드를 삽입하라.s.paint()
가 호출되면 "Shape"이 출력되도록 빈 칸에 적절한 코드를 삽입하라.동적 바인딩에 대해 다음에 답하라.
abstract class Shape {
public void paint() { draw(); }
abstract public void draw();
}
class Circle extends Shape {
private int radius;
public Circle(int radius) { this.radius = radius; }
double getArea() { return 3.14 * radius * radius; }
}
다음 중 오류가 발생하는 것을 있는 대로 골라라.
(1)Shape s;
(2)Shape s = new Shape;
(3)Circle c;
(4)Circle c = new Circle(10);
다음 코드의 실행 결과 "반지름=10"이 출력되도록 Circle 클래스를 수정하라.
Circle p = new Circle(10);
p.paint();
abstract class Shape {
public void paint() { draw(); }
abstract public void draw();
}
class Circle extends Shape {
private int radius;
public Circle(int radius) { this.radius = radius; }
double getArea() { return 3.14 * radius * radius; }
System.out.println("반지름="+radius);
}
다형성에 대한 설명 중 틀린 것은?
다음 중 인터페이스의 특징이 아닌 것은?
빈칸을 적절히 채우고, 실행 예시와 같이 출력되도록 클래스 TV에 필요한 메소드를 추가 작성하라.
________ Device {
void on();
void off();
}
public class TV _________ Device {
public static void main(String[] args) {
TV myTV = new TV();
myTV.on();
myTV.watch();
myTV.off();
}
}
켜졌습니다.
방송중입니다.
종료합니다.
interface Device {
void on();
void off();
}
public class TV implements Device {
public static void main(String[] args) {
TV myTV = new TV();
myTV.on();
myTV.watch();
myTV.off();
}
public void on() {
System.out.println("켜졌습니다.");
}
public void off() {
System.out.println("종료합니다.");
}
public void watch() {
System.out.println("방송중입니다.");
}
}