class Super {
String x = "Super : x";
String y = "Super : y";
void methodA() { System.out.println("Super : methodA()"); }
static void methodB() { System.out.println("Super : methodB()"); }
}
class Sub extends Super {
String x = "Sub : x";
String y = "Sub : y";
void methodA() { System.out.println("Sub : methodA()"); }
static void methodB() { System.out.println("Sub : methodB()"); }
}
public class Main2 {
public static void main(String[] args) {
Super obj = new Sub();
System.out.println(obj.x);
System.out.println(obj.y);
obj.methodA();
obj.methodB();
}
}
--------------------------------------------------
出力
--------------------------------------------------
Super : x
Super : y
Sub : methodA()
Super : methodB()
以下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