1.다음 코드에서 생성자로 인한 오류를 찾아내어 이유를 설명하고 오류를 수정하라.
class A {
private int a;
protected A(int i) { a = i; }
}
class B extends A {
private int b;
public B() { b = 0; }
}
- Javac builds default constructor when developer didnt write about the constructor of the class.
- Even child class should have default super constructor when developer didnt write the super constructor in child class.
- The only constructor in class A needs parameter int a. But in the default super constructor in class B doesnt have any parameter.
- So javac is not able to compile this code.
class A {
private int a;
protected A(int i) { a = i; }
}
class B extends A {
private int b;
public B() {
super(0);
b = 0;
}
}
2.다음 추상 클래스의 선언이나 사용이 잘못된 것을 있는 대로 가려내고 오류를 지적하라.
(1)abstract class A {
void f();
}
//
(2)abstract class A {
void f() { System.out.println("~"); }
}
//
(3)abstract class B {
abstract void f();
}
class C extends B {
}
//
(4)abstract class B {
abstract int f();
}
class C extends B {
void f() { System.out.println("~"); }
}
- (1): f() must have method body in the class A.
- (2): OK.
- (3): class C needs to implements the method body of f().
- (4): void f() must be int f() to override the abstract method f() in class B.