SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
Java研修
7.継承
山本
1.継承のおさらい
継承のクラス図
Parent
field1
field2
methodA
methodB
Child
methodA
methodB
サブクラス
(別名:派生クラス、子クラス、拡張クラス)
スーパークラス
(別名:基底クラス、親クラス)
Parent を継承したものがChild
class Child extends Parent {
…
}
サブクラスのインスタンス
Human
#name
#strengthLevel
+eat
+sleep
Programmer
-javaSkillLevel
+writesCode
+debugCode
クラス(設計図) インスタンス(オブジェクト)
kato
name
strengthLevel
javaSkillLevel
eat
sleep
writesCode
debugCode
サブクラスは、スーパーク
ラスのメンバ(フィールド、
メソッド)を引き継ぐ
Programmerクラスの
katoインスタンスを作成
2.継承の本質
継承の原則
• サブクラスはスーパーク
ラスのメンバを引き継ぐ
• スーパクラスの方がサブ
クラスより抽象度が高い
• 子クラス is 親クラス
– ○:柴犬 is 犬
– ○:柴犬 is 動物
– ×:犬 is 柴犬
– ×:動物 is 柴犬
動物
人間
プログラマ
犬
柴犬
抽
象
的
具
体
的
包含関係
動物
人間 犬
プログラマ 柴犬
チワワ
ラフコリー
柴犬
犬
動物
○:原則順 ×:原則と逆順
継承のメリット
• サブクラスはスーパー
クラスのメンバを引き継
ぐ
• 共通メンバをスーパー
クラスにまとめ、サブク
ラスは差分コーディン
グで済む
• 重複コードの排除
動物
名前
体力
人間 犬
食べる
寝る
関連性のない継承はダメ、ゼッタイ!
• メンバを利用したいからといって、安易に関連
性のない継承をしてはいけない
Human
name
age
eat
sleep
Dog
Human
name
age
eat
sleep
work
Dog
もしスーパークラスが
改修されたら…
仕事ができる犬が
誕生してしまう
おっ、ちょうどいいメ
ンバが揃ったクラス
があるじゃ~ん。
継承しよっと! class Dog extends Human { … }
多重継承はできない仕様
オレたち
オレンジ からたち
※ちなみにオレタチは本当にあります。
「オレンジ」と「からたち」を合成して「オレたち」っ
ていうキメラ果実をつくったぞ!
よし、両方の性質があるから2つ継承しよう!
(Javaの仕様上、許されていない)
演習7-1
• 以下の内、正しいコードはどれか。
1. Human kato1 = new Human();
2. Human kato2 = new Programmer();
3. Programmer kato3 = new Human();
4. Programmer kato4 = new Programmer();
Human
Programmer
正しい
→コンパイルできる
宿題
• 加藤さん2号はいったい何者なの?
– Human kato2 = new Programmer();
3.引き継ぎをもっと詳しく
引き継がないもの
• 原則
– サブクラスはスーパークラスのメンバを引き継ぐ
• 例外
– コンストラクタ → 動作がトリッキー
演習7-2
• Run_7_2クラスのmainメソッドを実行せよ。
– Human, Programmerクラスともに、以下のコンスト
ラクタを実装している
Human() {
System.out.println("Humanクラスのコンストラクタが実行されました。");
}
Programmer() {
System.out.println("Programmerクラスのコンストラクタが実行されました。");
}
Human
Programmer
サブクラスをnewしたときに起こること
1. スーパークラスのインスタンス生成
2. サブクラスのインスタンス生成
– スーパークラスのメンバ引継
3. スーパークラスのコンストラクタ実行
– サブクラスにコンストラクタがあると、最初にスパ
ークラスのコンストラクタを呼び出す
– サブクラスで明示しない限り、引数なしのコンス
トラクタが呼び出される
4. サブクラスのコンストラクタ実行
演習7-3
• Programmerクラスに以下のコンストラクタを
実装せよ。
Human
#name: String
#strengthLevel: int
Programmer
-javaSkillLevel: int
第1引数 String型 → Humanクラスのnameフィールドへ代入
第2引数 int型 → HumanクラスのstrengthLevelフィールドへ代入
第3引数 int型 → ProgrammerクラスのjavaSkillLevelフィールドへ代入
Programmerコンストラクタ
//加藤さん4号インスタンス作成
Programmer kato4 = new Programmer("加藤",100, 60);
参考)mainメソッドのインスタンス生成文
オーバーライド
• スーパークラスのメソッドを、サ
ブクラスで再定義可能
– Programmerクラスでは、Human
クラスとは異なるeat, sleepメソッ
ドの実装ができる
• オーバーライドの条件
– メソッド名や、引数、戻り値を揃
える必要がある
Human
#name
#strengthLevel
+eat
+sleep
Programmer
+eat
+sleep
演習7-3
• Run_7_3クラスのmainメソッドを実行
せよ。
• Programmerクラスに以下のメソッドを
オーバーライドし、再実行せよ。
– eatメソッド
• プログラマは多忙を極め、食事はコンビ
ニ弁当等の栄養に偏りがあるもので済
ませがちであるため、体力の回復量を
Humanクラスの半分にせよ。
– sleepメソッド
• プログラマは多忙を極め、帰宅が遅く、
睡眠時間が短くなりがちなため、体力の
回復量をHumanクラスの半分にせよ。
Human
#name
#strengthLevel
+eat
+sleep
Programmer
-javaSkillLevel
+writesCode
+debugCode
4.実装しない継承
抽象クラスと抽象メソッド
• 抽象クラス
– abstract修飾子がついたクラス
– インスタンスの作成不可
→インスタンス化はサブクラスに委ねる
• 抽象メソッド
– abstract修飾子がついたメソッド
– 実装不可
→実装はサブクラスのオーバーライドに委ねる
そしてインタフェースへ…

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Java研修 継承