pi-9. Javaプログラミングにおけ
るクラス継承
1
金子邦彦
(Java マスター講座:基礎から応用まで)
URL: https://www.kkaneko.jp/pro/pi/index.html
Javaにおけるクラス継承の仕組み、スーパークラスと
サブクラスの関係、継承を活用したプログラムの作成
【学習内容の構成】
1. スーパークラスとサブクラス:サブクラスのオブ
ジェクトはすべてスーパークラスに属するという包
含関係
2. 継承の基本:extendsによるスーパークラス指定、
superによるコンストラクタ呼び出し、属性とメソッ
ドの受け継ぎ
3. メソッドのオーバーライド:サブクラスでスーパー
クラスのメソッドを別の定義に置き換える機能
4. 属性の追加:サブクラスでスーパークラスにない属
性やメソッドを追加する拡張
• 前提:Javaのクラス定義、オブジェクト生成、メソッ
ドアクセスの基礎
• 意義:コードの再利用性向上、オブジェクト指向設計
の理解
2
クラスとオブジェクト
クラスは,同じ種類のオブジェクトの集まりと考え
ることができる
3
人間
学生
学生でもあり人間でもある
人間だが,学生ではない
4
半径 3,場所(2, 4)
色 green
半径 1,場所(8, 10)
色 blue
クラス定義のプログラム
クラス Ball
オブジェクト
オブジェクト
オブジェクト生成のプログラム
メソッドアクセスのプログラム
Ball a = new Ball(8, 10, 1, "blue");
Ball b = new Ball(2, 4, 3, "green");
a.printout();
b.printout();
Java のクラス
クラス定義のプログラム
5
クラス名: Ball
メソッド: Ball
メソッド: printout
このクラス定義を使用した,オブジェクトの生成
a 8 10 1 "red"
b 2 4 3 "green"
x y r color
Javaプログラム
Ball a = new Ball(8, 10, 1, "blue");
Ball b = new Ball(2, 4, 3, "green");
4つの属性
メソッドアクセス
6
a や b オブジェクト
printout() メソッド
間を「.」で区切っている
Javaプログラム
a.printout();
b.printout();
Java Tutor の起動
① ウェブブラウザを起動する
② Java Tutor を使いたいので,次の URL を開く
https://pythontutor.com/java.html
③ 「Java」をクリック ⇒ 編集画面が開く
7
Java Tutor でのプログラム実行手順
8
(1)「Visualize Execution」をク
リックして実行画面に切り替える
(2)「Last」をクリック.
(3) 実行結果を確認する.
(4)「Edit this code」をク
リックして編集画面に戻る
Java Tutor 使用上の注意点①
• 実行画面で,次のような赤の表示が出ることがある →
無視してよい
過去の文法ミスに関する確認表示
邪魔なときは「Close」
9
Java Tutor 使用上の注意点②
「please wait ... executing」のとき,10秒ほど待つ.
→ 混雑しているときは, 「Server Busy・・・」
というメッセージが出ることがある.
混雑している.少し(数秒から数十秒)待つと自
動で表示が変わる(変わらない場合には,操作を
もう一度行ってみる)
10
9-1. スーパークラス,サブク
ラス
11
スーパークラスとサブクラス
正方形は,長方形である
(正方形ではない長方形もある)
12
長方形: スーパークラス
正方形: サブクラス
長方形と正方形
13
幅 2,高さ 1,
場所(8, 10)
幅 1,高さ 2,
場所(4, 8)
幅 3,高さ 1,
場所(7, 5)
幅 1,高さ 1,
場所(0, 3)
幅 2,高さ 2,
場所(1, 1)
長方形と正方形
14
幅 2,高さ 1,
場所(8, 10)
幅 1,高さ 2,
場所(4, 8)
幅 3,高さ 1,
場所(7, 5)
幅 1,高さ 1,
場所(0, 3)
幅 2,高さ 2,
場所(1, 1)
長方形であるが,
正方形ではない
長方形であり,
正方形でもある
15
幅 2,高さ 1,
場所(8, 10)
幅 1,高さ 2,
場所(4, 8)
幅 3,高さ 1,
場所(7, 5)
幅 1,高さ 1,
場所(0, 3)
幅 2,高さ 2,
場所(1, 1)
「正方形」の
クラス
「長方形」の
クラス
スーパークラスとサブクラス
「正方形」はすべて長方形
である
「正方形」ではない
長方形もある
スーパークラス,サブクラス
サブクラスのオブジェクトは,すべてスーパー
クラスに属する
16
クラス X (スーパークラス)
クラス Y (サブクラス)
スーパークラス,サブクラスの例
例①
くだもの りんご
スーパークラス サブクラス
例②
住民 世帯主
スーパークラス サブクラス
例③
図形 円
スーパークラス サブクラス
17
9-2. スーパークラス,サブク
ラスの Java プログラム,継
承
18
Java でのクラス定義
• class Y extends X : クラス Y のスーパークラス
はX であることを指定
• super : スーパークラスのコンストラクタの呼び
出し
19
class X {
・・・
public X(・・・) {
・・・
}
・・・
}
class Y extends X {
・・・
public Y(・・・) {
super(・・・)
・・・
}
・・・
}
クラス定義
クラス定義
Y のスーパークラスが
X である場合
「長方形」のクラス定義の例
20
クラス名: Rectangle
メソッド: Rectangle
メソッド: printout
このクラス定義を使用した,オブジェクトの生成
a 4 8 1 2
b 8 10 2 1
x y width height
Javaプログラム
Rectangle a = new Rectangle(4, 8, 1, 2);
Rectangle b = new Rectangle(8, 10, 2, 1);
4つの属性
継承
• 継承:スーパークラスの属性とメソッドをサブク
ラスが受け継ぐ
• 但し,コンストラクタは受け継がない
(コンストラクタは必ず定義する必要がある)
21
長方形 (Rectangle) 正方形 (Square)
属性とメソッド 継承 属性 とメソッド
x x
y y
width width
height height
printout printout
スーパークラス サブクラス
「正方形」のクラス定義の例
22
クラス名: Square
メソッド: Square
このクラス定義を使用した,オブジェクトの生成
c 0 3 1 1
d 1 1 2 2
x y width height
Javaプログラム
Square c = new Square(0, 3, 1);
Square d = new Square(1, 1, 2);
スーパークラス: Rectangle
キーワード
23
extends スーパークラスの指定
super スーパークラスのコンストラクタの
呼び出し
演習
資料:26 ~ 28
【トピックス】
• スーパークラス
• サブクラス
• extends
• super
24
25
① Java Tutor のエディタで次のプログラムを入れる
26
② 引き続き,次のプログラムを入れる
③ 実行し,結果を確認する
(あとで使うので、プログラムを消さないこと)
27
4つのオブジェクト
a, b, c, d が生成される
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
9-3. メソッドのオーバーライ
ド
28
メソッドのオーバーライド
• コンストラクタ以外のメソッドは,受け継がずに
オーバーライドできる
オーバーライドは,別の定義を行うこと
29
長方形 (Rectangle) 正方形 (Square)
属性とメソッド 継承 属性 とメソッド
x x
y y
width width
height height
printout printout ← オーバーライド
スーパークラス サブクラス
「正方形」のクラス定義の例
30
クラス名: Square
メソッド: Square
メソッド: printout
スーパークラス: Rectangle
• メソッド printout のオーバーライド
• スーパークラスのメソッド printout とは別の定義
を行う。
演習
資料:33 ~ 34
【トピックス】
• メソッドのオーバーライド
31
32
① Java Tutor のエディタで次のプログラムを加える
② 実行し,結果を確認する
(あとで使うので、プログラムを消さないこと)
33
4つのオブジェクト
a, b, c, d が生成される
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
表示が変わる
9-4. サブクラスでの属性の追
加
34
サブクラスでの属性,メソッドの追加
• サブクラスで,スーパークラスにない属性やメ
ソッドを追加できる
35
長方形 (Rectangle) 色付きの長方形 (ColorRectangle)
属性とメソッド 継承 属性 とメソッド
x x
y y
width width
height height
printout printout
スーパークラス color サブクラスで追加された属性
サブクラス
「色付きの長方形」のクラス定義の例
36
クラス名: ColorRectangle
メソッド: Square
メソッド: printout
スーパークラス: Rectangle
• サブクラスで,属性 color を追加
演習
資料:39 ~ 41
【トピックス】
• サブクラスでの属性の追加
37
38
① Java Tutor のエディタで次のプログラムを加える
39
② 引き続き,次のプログラムを加える
③ 実行し,結果を確認する
40
5つのオブジェクト
a, b, c, d, e が生成される
「Visual Execution」をクリック.そして「Last」をクリック.結果を確認.
「Edit this code」をクリックすると,エディタの画面に戻る
全体まとめ
• サブクラスのオブジェクトは,すべてスーパーク
ラスに属する
• 継承:スーパークラスの属性とメソッドをサブク
ラスが受け継ぐ
但し,コンストラクタは受け継がない
• コンストラクタ以外のメソッドは,オーバーライ
ドできる
• サブクラスで,スーパークラスにない属性やメ
ソッドを追加できる
41
関連ページ
• Java プログラミング入門
GDB online を使用
https://www.kkaneko.jp/pro/ji/index.html
• Java の基本
Java Tutor, GDB online を使用
https://www.kkaneko.jp/pro/pi/index.html
• Java プログラム例
https://www.kkaneko.jp/pro/java/index.html
42
ソースコード (9-2)
class Rectangle {
double x;
double y;
double width;
double height;
public Rectangle(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.width);
System.out.println(this.height);
}
}
class Square extends Rectangle {
public Square(double x, double y, double size) {
super(x, y, size, size);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Rectangle a = new Rectangle(4, 8, 1, 2);
Rectangle b = new Rectangle(8, 10, 2, 1);
a.printout();
b.printout();
Square c = new Square(0, 3, 1);
Square d = new Square(1, 1, 2);
c.printout();
d.printout();
}
}
43
ソースコード (9-3)
class Rectangle {
double x;
double y;
double width;
double height;
public Rectangle(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.width);
System.out.println(this.height);
}
}
class Square extends Rectangle {
public Square(double x, double y, double size) {
super(x, y, size, size);
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.width);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Rectangle a = new Rectangle(4, 8, 1, 2);
Rectangle b = new Rectangle(8, 10, 2, 1);
a.printout();
b.printout();
Square c = new Square(0, 3, 1);
Square d = new Square(1, 1, 2);
c.printout();
d.printout();
}
}
44
ソースコード (9-4)
class Rectangle {
double x;
double y;
double width;
double height;
public Rectangle(double x, double y, double width, double height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.width);
System.out.println(this.height);
}
}
class Square extends Rectangle {
public Square(double x, double y, double size) {
super(x, y, size, size);
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.width);
}
}
class ColorRectangle extends Rectangle {
String color;
public ColorRectangle(double x, double y, double width, double height, String color) {
super(x, y, width, height);
this.color = color;
}
public void printout() {
System.out.println(this.x);
System.out.println(this.y);
System.out.println(this.width);
System.out.println(this.height);
System.out.println(this.color);
}
}
public class YourClassNameHere {
public static void main(String[] args) {
Rectangle a = new Rectangle(4, 8, 1, 2);
Rectangle b = new Rectangle(8, 10, 2, 1);
a.printout();
b.printout();
Square c = new Square(0, 3, 1);
Square d = new Square(1, 1, 2);
c.printout();
d.printout();
ColorRectangle e = new ColorRectangle(0, 0, 1, 4, "Red");
e.printout();
}
}
45