SlideShare a Scribd company logo
1 of 7
Inheritance
COMPLETE JAVA MASTERCLASS
Inheritance - Part 1
What is Inheritance, and why is it so powerful?
We can look at Inheritance as a form of code re-use.
It's a way to organize classes into a parent-child hierarchy, which lets the child inherit (re-use), fields and methods
from its parent.
Inheritance
COMPLETE JAVA MASTERCLASS
Inheritance - Part 1
Each box on this diagram represents a Class.
The most generic, or base class, starts at the top of the
hierarchy.
Every class below it is a subclass.
So Animal is the base class. All the other classes can be
said to be subclasses of Animal.
A parent can have multiple children, as we see with Mammal,
which is the parent of Dog and Cat.
A child can only have one direct parent, in Java
But it will inherit from its parent class's parent, and so on.
The Animal class
COMPLETE JAVA MASTERCLASS
Inheritance - Part 1
A class diagram, allows us to design our classes before we build them.
This diagram shows the Animal class, with the attributes we think that every kind of animal has.
Below the fields, we have the behavior that animals have in common, move, and makeNoise.
Class Model for Animal and Dog
COMPLETE JAVA MASTERCLASS
Inheritance - Part 1
Dog inherits from Animal.
In other words, Dog 'IS A' type of Animal.
When we create a Dog object, it will inherit Animal's attributes (type, size
and weight).
This is also true for Animal's methods. Dog will inherit these as well.
We can specialize the Dog class with its own fields and behavior.
extends
COMPLETE JAVA MASTERCLASS
Inheritance - Part 1
Using extends specifies the superclass (or the parent class) of the class we're declaring.
We can say Dog is a subclass, or child class, of Animal.
We can say Animal is a parent, or super class, of Dog.
A class can specify one, and only one, class in its extends clause.
super()
COMPLETE JAVA MASTERCLASS
Inheritance - Part 1
super() is a lot like this().
It's a way to call a constructor on the super class, directly from the sub class's constructor.
Like this(), it has to be the first statement of the constructor.
Because of that rule, this() and super() can never be called from the same constructor.
super()
COMPLETE JAVA MASTERCLASS
Inheritance - Part 1
If you don't make a call to super(), then Java makes it for you, using super's default constructor.
If your super class doesn't have a default constructor, than you must explicitly call super() in all of your
constructors, passing the right arguments to that constructor.

More Related Content

Similar to 88 Inheritance - Part 1.pptx

Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdfkumari36
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java yash jain
 
some basic knowledge about Java programming
some basic knowledge about Java programming some basic knowledge about Java programming
some basic knowledge about Java programming ITTraining2
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Designİbrahim Kürce
 
Java object oriented programming - OOPS
Java object oriented programming - OOPSJava object oriented programming - OOPS
Java object oriented programming - OOPSrithustutorials
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
Final Class in Java - By JavaGoal
Final Class in Java - By JavaGoalFinal Class in Java - By JavaGoal
Final Class in Java - By JavaGoalJavaGoal
 
Application Animal Characteristics In today’.docx
           Application Animal Characteristics In today’.docx           Application Animal Characteristics In today’.docx
Application Animal Characteristics In today’.docxhallettfaustina
 
Ayan Das_25300121057.pptx
Ayan Das_25300121057.pptxAyan Das_25300121057.pptx
Ayan Das_25300121057.pptxAyan974999
 
Chapter2 java oop
Chapter2 java oopChapter2 java oop
Chapter2 java oopsshhzap
 
Object Oriented Relationships
Object Oriented RelationshipsObject Oriented Relationships
Object Oriented RelationshipsTaher Barodawala
 
Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxnaeemcse
 
Polymorphism.ppt
Polymorphism.pptPolymorphism.ppt
Polymorphism.pptEmanAsem4
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in javaTech_MX
 

Similar to 88 Inheritance - Part 1.pptx (20)

Inheritance in Java.pdf
Inheritance in Java.pdfInheritance in Java.pdf
Inheritance in Java.pdf
 
Inheritance in java
Inheritance in java Inheritance in java
Inheritance in java
 
some basic knowledge about Java programming
some basic knowledge about Java programming some basic knowledge about Java programming
some basic knowledge about Java programming
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
OCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class DesignOCA Java SE 8 Exam Chapter 5 Class Design
OCA Java SE 8 Exam Chapter 5 Class Design
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Java object oriented programming - OOPS
Java object oriented programming - OOPSJava object oriented programming - OOPS
Java object oriented programming - OOPS
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Final Class in Java - By JavaGoal
Final Class in Java - By JavaGoalFinal Class in Java - By JavaGoal
Final Class in Java - By JavaGoal
 
Application Animal Characteristics In today’.docx
           Application Animal Characteristics In today’.docx           Application Animal Characteristics In today’.docx
Application Animal Characteristics In today’.docx
 
Ayan Das_25300121057.pptx
Ayan Das_25300121057.pptxAyan Das_25300121057.pptx
Ayan Das_25300121057.pptx
 
Chapter2 java oop
Chapter2 java oopChapter2 java oop
Chapter2 java oop
 
Object Oriented Relationships
Object Oriented RelationshipsObject Oriented Relationships
Object Oriented Relationships
 
Inheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptxInheritance in Java beginner to advance with examples.pptx
Inheritance in Java beginner to advance with examples.pptx
 
Polymorphism.ppt
Polymorphism.pptPolymorphism.ppt
Polymorphism.ppt
 
Computer Programming 2
Computer Programming 2 Computer Programming 2
Computer Programming 2
 
Java inheritance
Java inheritanceJava inheritance
Java inheritance
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
4th_class.pdf
4th_class.pdf4th_class.pdf
4th_class.pdf
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 

Recently uploaded

Essential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonEssential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonMayur Khatri
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxCapitolTechU
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resourcesaileywriter
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the lifeNitinDeodare
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTechSoup
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxJenilouCasareno
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesRased Khan
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticspragatimahajan3
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Mohamed Rizk Khodair
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...Nguyen Thanh Tu Collection
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Celine George
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfQucHHunhnh
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxnuriaiuzzolino1
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/siemaillard
 
The Ultimate Guide to Social Media Marketing in 2024.pdf
The Ultimate Guide to Social Media Marketing in 2024.pdfThe Ultimate Guide to Social Media Marketing in 2024.pdf
The Ultimate Guide to Social Media Marketing in 2024.pdfdm4ashexcelr
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringDenish Jangid
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17Celine George
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesashishpaul799
 

Recently uploaded (20)

Essential Safety precautions during monsoon season
Essential Safety precautions during monsoon seasonEssential Safety precautions during monsoon season
Essential Safety precautions during monsoon season
 
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptxslides CapTechTalks Webinar May 2024 Alexander Perry.pptx
slides CapTechTalks Webinar May 2024 Alexander Perry.pptx
 
The Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational ResourcesThe Benefits and Challenges of Open Educational Resources
The Benefits and Challenges of Open Educational Resources
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdfPost Exam Fun(da) Intra UEM General Quiz - Finals.pdf
Post Exam Fun(da) Intra UEM General Quiz - Finals.pdf
 
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdfTelling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
Telling Your Story_ Simple Steps to Build Your Nonprofit's Brand Webinar.pdf
 
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptxMatatag-Curriculum and the 21st Century Skills Presentation.pptx
Matatag-Curriculum and the 21st Century Skills Presentation.pptx
 
Application of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matricesApplication of Matrices in real life. Presentation on application of matrices
Application of Matrices in real life. Presentation on application of matrices
 
size separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceuticssize separation d pharm 1st year pharmaceutics
size separation d pharm 1st year pharmaceutics
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
BỘ LUYỆN NGHE TIẾNG ANH 8 GLOBAL SUCCESS CẢ NĂM (GỒM 12 UNITS, MỖI UNIT GỒM 3...
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdfDanh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
Danh sách HSG Bộ môn cấp trường - Cấp THPT.pdf
 
Mbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptxMbaye_Astou.Education Civica_Human Rights.pptx
Mbaye_Astou.Education Civica_Human Rights.pptx
 
Championnat de France de Tennis de table/
Championnat de France de Tennis de table/Championnat de France de Tennis de table/
Championnat de France de Tennis de table/
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
The Ultimate Guide to Social Media Marketing in 2024.pdf
The Ultimate Guide to Social Media Marketing in 2024.pdfThe Ultimate Guide to Social Media Marketing in 2024.pdf
The Ultimate Guide to Social Media Marketing in 2024.pdf
 
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & EngineeringBasic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
Basic Civil Engg Notes_Chapter-6_Environment Pollution & Engineering
 
How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17How to the fix Attribute Error in odoo 17
How to the fix Attribute Error in odoo 17
 
ppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyesppt your views.ppt your views of your college in your eyes
ppt your views.ppt your views of your college in your eyes
 

88 Inheritance - Part 1.pptx

  • 1. Inheritance COMPLETE JAVA MASTERCLASS Inheritance - Part 1 What is Inheritance, and why is it so powerful? We can look at Inheritance as a form of code re-use. It's a way to organize classes into a parent-child hierarchy, which lets the child inherit (re-use), fields and methods from its parent.
  • 2. Inheritance COMPLETE JAVA MASTERCLASS Inheritance - Part 1 Each box on this diagram represents a Class. The most generic, or base class, starts at the top of the hierarchy. Every class below it is a subclass. So Animal is the base class. All the other classes can be said to be subclasses of Animal. A parent can have multiple children, as we see with Mammal, which is the parent of Dog and Cat. A child can only have one direct parent, in Java But it will inherit from its parent class's parent, and so on.
  • 3. The Animal class COMPLETE JAVA MASTERCLASS Inheritance - Part 1 A class diagram, allows us to design our classes before we build them. This diagram shows the Animal class, with the attributes we think that every kind of animal has. Below the fields, we have the behavior that animals have in common, move, and makeNoise.
  • 4. Class Model for Animal and Dog COMPLETE JAVA MASTERCLASS Inheritance - Part 1 Dog inherits from Animal. In other words, Dog 'IS A' type of Animal. When we create a Dog object, it will inherit Animal's attributes (type, size and weight). This is also true for Animal's methods. Dog will inherit these as well. We can specialize the Dog class with its own fields and behavior.
  • 5. extends COMPLETE JAVA MASTERCLASS Inheritance - Part 1 Using extends specifies the superclass (or the parent class) of the class we're declaring. We can say Dog is a subclass, or child class, of Animal. We can say Animal is a parent, or super class, of Dog. A class can specify one, and only one, class in its extends clause.
  • 6. super() COMPLETE JAVA MASTERCLASS Inheritance - Part 1 super() is a lot like this(). It's a way to call a constructor on the super class, directly from the sub class's constructor. Like this(), it has to be the first statement of the constructor. Because of that rule, this() and super() can never be called from the same constructor.
  • 7. super() COMPLETE JAVA MASTERCLASS Inheritance - Part 1 If you don't make a call to super(), then Java makes it for you, using super's default constructor. If your super class doesn't have a default constructor, than you must explicitly call super() in all of your constructors, passing the right arguments to that constructor.