final 예약어
- 변수 : 값을 변경할 수 없다.
- 메서드 : 메서드 오버라이딩(재정의)
- 클래스 : 상속할 수 없다
package test;
public final class Constant {
public int num = 10;
public final int NUM = 100;
public static final double PI=3.14;
public final void prn() {
System.out.println("Constant prn()");
}
}
package test;
public class SubConstant {
}
package test;
public class Test1 {
public static void main(String[] args) {
Constant c = new Constant();
System.out.println(c.num);
System.out.println(c.NUM);
c.num=50;
System.out.println(Constant.PI)
}
}
인터페이스 연습
package test;
public interface Calc {
double PI = 3.14;
int ERR = -9999;
int add(int num1, int num2);
int sub(int num1, int num2);
int mul(int num1, int num2);
int div(int num1, int num2);
package test;
public interface Calc2 {
public abstract void prn();
}
package test;
public class Calculator implements Calc2 {
@Override
public int add(int num1, int num2) {
return num1+num2;
}
@Override
public int sub(int num1, int num2) {
return num1-num2;
}
@Override
public int mul(int num1, int num2) {
return num1*num2;
}
@Override
public int div(int num1, int num2) {
if(num2!=0) {
return num1/num2;
}else {
return Calc.ERR;
}
@Override
public void prn() {
System.out.println("메서드 오버라이딩");
}
}
package test;
public class Test2 {
public static void main(String[] args) {
Calc c= new Calculator();
System.out.println(c.add(10, 20));
System.out.println(c.sub(10, 20));
System.out.println(c.mul(10, 20));
System.out.println(c.div(10, 20));
System.out.println(c.div(10, 0));
Calc2 c2 = new Calculator();
c2.prn();
}
}