以下2つのコードの内容は同じ
--------------------------------------------------
interface Calc {
int NUM1 = 10;
int NUM2 = 50;
void calc();
}
class Add implements Calc {
public void calc() {
System.out.println(NUM1 + NUM2);
}
}
class Sub implements Calc {
public void calc() {
System.out.println(NUM1 - NUM2);
}
}
public class Main {
public static void main(String[] args) {
Add add = new Add();
add.calc();
Sub sub = new Sub();
sub.calc();
}
}
--------------------------------------------------
class Calc {
int NUM1 = 10;
int NUM2 = 50;
public void add() {
System.out.println(NUM1 + NUM2);
}
public void sub() {
System.out.println(NUM1 - NUM2);
}
}
public class Main {
public static void main(String[] args) {
Calc cal = new Calc();
cal.add();
cal.sub();
}
}
--------------------------------------------------
出力
--------------------------------------------------
60
-40
abstract class X { // 抽象クラス
protected abstract void methodA();
}
abstract class Y extends X { } // 抽象クラス
class Z extends Y { // 具象クラス
protected void methodA() { } // 具象クラスではオーバーライドは必須
}
class SuperA {
public SuperA() { System.out.println("SuperA()"); }
public SuperA(int a) { System.out.println("SuperA(int a)"); }
}
class SubA extends SuperA {
public SubA() { System.out.println("SubA()"); }
public SubA(int a) {
super(a); // 引数付きコンストラクタを呼び出すのに必要な記述
System.out.println("SubA(int a)"); }
}
public class Main {
public static void main(String[] args) {
SubA obj1 = new SubA();
SubA obj2 = new SubA(10);
}
}
--------------------------------------------------
出力
--------------------------------------------------
SuperA()
SubA()
SuperA(int a)
SubA(int a)