SlideShare a Scribd company logo
O
O
P
Inheritance
Object Oriented Programming
Prepared & Presented by:
Mahmoud Rafeek Alfarra
2012
Chapter 3
OO
OO
PP
http://mfarra.cst.ps
Contents
What is Inheritance ? Advantages1
Superclasses and Subclasses2
"is-a" and the "has-a" relationship3
Mini Example: Vehicle4
Constructors in Subclasses5
Example: Students’ types6
Single Vs Multiple inheritance7
Important Notes8
OO
OO
PP
What is Inheritance ?
Inheritance is a form of software reuse in
which a new class is created by absorbing
an existing class's members and
embellishing them with new or modified
capabilities.
http://mfarra.cst.ps
P_Properties
P_ methods
P_Properties
P_ methods Parent Class
C_Properties
C_methods
C_Properties
C_methods Child Class
This class has its
properties, methods
and that of its parent.
This class has its
properties, methods
and that of its parent.
OO
OO
PP
Advantages of Inheritance
http://mfarra.cst.ps
Using inheritance:
 to minimize the amount of duplicate
code.
 A better organization of code and
smaller, simpler compilation units.
 make application code more flexible to
change because classes that inherit
from a common superclass can be
used interchangeably.
OO
OO
PP
Superclasses and Subclasses
http://mfarra.cst.ps
P_Properties
P_ methods
P_Properties
P_ methods SuperClass
C_Properties
C_methods
C_Properties
C_methods SubClass
 When creating a class, rather than
declaring completely new members, the
programmer can designate that the new
class should inherit the members of an
existing class.
 The existing class is called the
superclass, and the new class is the
subclass.
 Each subclass can become the
superclass for future subclasses.
OO
OO
PP
Superclasses and Subclasses
http://mfarra.cst.ps
A subclass normally adds its own fields
and methods.
Therefore, a subclass is more specific
than its superclass.
Typically, the subclass exhibits the
behaviors of its superclass and
additional behaviors that are specific to
the subclass.
In Java, the class hierarchy begins with class
Object (in package java.lang), which every class
in Java directly or indirectly extends.
OO
OO
PP
properties3
methods3
properties3
methods3 SubClass
Direct & Indirect Superclasses
http://mfarra.cst.ps
The direct superclass is
the superclass from
which the subclass
explicitly inherits.
 An indirect superclass
is any class above the
direct superclass in the
class hierarchy.
properties1
methods1
properties1
methods1 Indirect
SuperClass
properties2
methods2
properties2
methods2 Direct
SuperClass
OO
OO
PP
"is-a" and the "has-a" relationship
http://mfarra.cst.ps
"Is-a" represents inheritance.
In an "is-a" relationship, an object of a
subclass can also be treated as an object
of its superclass.
For example, a car is a vehicle.
Vehicle
Class
Vehicle
Class SuperClass
Car ClassCar Class SubClass
Honda is a car &
Honda is a vehicle
Honda is a car &
Honda is a vehicle
Object of sub is also an object of superObject of sub is also an object of super
OO
OO
PP
Mini Example: VehicleProject
http://mfarra.cst.ps
class Vehicle {
protected String model;
protected float price;
public Vehicle(String model, float price){
this.model = model;
this.price = price;
}
public String print(){
return "Model: "+model+"t Price: "+price;
}
public void setModel(String model){
this.model = model;
}
public String getModel(){
return model;
} // set and get of price
}
OO
OO
PP
Mini Example: Car class
http://mfarra.cst.ps
class Car extends Vehicle{
private int passengers;
public Car(String model, float price,int passengers){
super( model, price);
this.passengers = passengers;
}
public String print(){
return "Data is:n"+super.print()+
" # of passengers: "+passengers;
}
}
A compilation error occurs if a subclass constructor calls one
of its superclass constructors with arguments that do not
match the superclass constructor declarations.
OO
OO
PP
Mini Example: using sub & super class
http://mfarra.cst.ps
public class VehicleProjectInheritance {
public static void main(String[] args) {
Car c = new Car("Honda", 455.0f, 4);
System.out.println(c.print());
}
}
OO
OO
PP
Constructors in Subclasses
 When a program creates a subclass object, the
subclass constructor immediately calls the
superclass constructor.
 The superclass constructor's body executes to
initialize the superclass's instance variables that are
part of the subclass object, then the subclass
constructor's body executes to initialize the
subclass-only instance variables.
http://mfarra.cst.ps
OO
OO
PP
Constructors in Subclasses
http://mfarra.cst.ps
Public Test1(){
}
Public Test1(){
}
SuperClass
Public Test2(){
}
Public Test2(){
}
SubClass
calls
Return values
Subclass constructor invokes its direct superclass's
constructor either explicitly (via the super reference) or
implicitly (calling the superclass's default constructor or no-
argument constructor).
OO
OO
PP
Example: Students’ types
http://mfarra.cst.ps
• Have part time hours • Have a field training
OO
OO
PP
Example: Student Class
http://mfarra.cst.ps
class Student {
protected String name;
protected String mobile;
protected float gpa;
public Student(String name, String mobile, float gpa){
this.name = name;
this.mobile = mobile;
this.gpa = gpa;
}
// set methods
public void setName(String name){
this.name= name;
}
…
// get methods
public String getName(){
return name;
}
….
// Print Data
public String showData(){
return "Name: "+getName()+"nMobile: "+getMobile()+"nGPA: "+getGpa();
} }
OO
OO
PP
Example: PostGraduated Class
http://mfarra.cst.ps
class PostGraduated extends Student {
private int hours;
public PostGraduated(String name, String mobile, float gpa,int hours){
super(name, mobile, gpa);
this.hours = hours;
}
// set method
public void setHours(int hours){
this.hours = hours;
}
// get method
public int getHours(){
return hours;
}
public String PrintData (){
return showData() + " Hours: "+getHours();
}
}
Declare another
method to calculate
the earn of post
graduate if the cost of
each hour is 20$.
OO
OO
PP
Example: Graduate Class
http://mfarra.cst.ps
class Graduate extends Student {
private String trainingField;
public Graduate(String name, String mobile, float gpa, String trainingField){
super(name, mobile, gpa);
this.trainingField = trainingField;
}
public void setTrainingField(String trainingField){
this.trainingField= trainingField;
}
public String setTrainingField(){
return trainingField;
}
public String Printinfo(){
return showData() + " TrainingField: "+getTrainingField();
}
}
Declare another
method to calculate
the grade of the
graduate student
(Excellent, V.Good, …)
OO
OO
PP
Method Overridden
http://mfarra.cst.ps
An instance method in a subclasssubclass with the
same signature (name, plus the number and the
type of its parameters) and return type as an
instance method in the superclasssuperclass overrides
the superclass's method.
public class Animal {
public static void testClassMethod() {
System.out.println("The class" + " method in Animal"); }
public void testInstanceMethod() {
System.out.println("The instance " + " method in Animal."); }
}
public class Cat extends Animal {
public static void testClassMethod() {
System.out.println("The class method" + " in Cat."); }
public void testInstanceMethod() {
System.out.println("The instance method" + " in Cat.");
}
Overrideby
Overrideby
OO
OO
PP
Method Overridden
http://mfarra.cst.ps
public int calcsal(int x){
sal = days*x;
return sal;
}
public int calcsal(int x){
sal = days*x;
return sal;
}
public int calcsal(int x){
sal = (days*x)- absent;
return sal;
}
public int calcsal(int x){
sal = (days*x)- absent;
return sal;
}
Super obj1 = new Super ();
int x = obj.
Super obj1 = new Super ();
int x = obj. Calcsal(20);Calcsal(20);
Super class
Sub class
Sub obj2 = new Super ();
int x = obj.
Sub obj2 = new Super ();
int x = obj. Calcsal(20);Calcsal(20);
OO
OO
PP
Method Overridden Vs Method Overloading
http://mfarra.cst.ps
Using code, distinguish between overridden and
overloading method.
Implement the concept of method overridden on the
methods of print in example of Students’ types
OO
OO
PP
Example: Inherited members from Student Class
http://mfarra.cst.ps
Inherited method
from student class
Inherited method
from student class
OO
OO
PP
Single Vs Multiple inheritance
Class 1Class 1 Class 2Class 2
Class 3Class 3
Class 1Class 1
Class 2Class 2
Multiple Inheritance Single Inheritance
Class can inherit members from more than
one class (more than one super class)
Class can inherit members from only
one class (one super class)
Java was designed without
multiple inheritance.
Java was designed without
multiple inheritance.
Java was designed with
single inheritance.
Java was designed with
single inheritance.
OO
OO
PP
Important Notes
Methods of a subclass cannot directly access
private members of their superclass.
With inheritance, the common instance variables
and methods of all the classes in the hierarchy
are declared in a superclass.
Use the protected access modifier when a superclass
should provide a method only to its subclasses and
other classes in the same package, but not to other
clients.
A subclass is more specific than its superclass
and represents a smaller group of objects.
A superclass's protected members have an
intermediate level of protection between public and
private access. They can be accessed by members of
the superclass, by members of its subclasses and by
members of other classes in the same package.
OO
OO
PP
‫ربكم‬ ‫استغفروا‬
:‫ذكره‬ ‫تعالى‬ ‫ا‬ ‫قال‬
‫بوا‬ُ ‫تو‬ُ ‫م‬ّ ‫ث‬ُ ‫م‬ْ ‫ك‬ُ ‫ب‬ّ ‫ر‬َ ‫روا‬ُ ‫ف‬ِ ‫غ‬ْ ‫ت‬َ ‫س‬ْ ‫ا‬ ‫ن‬ِ ‫أ‬َ ‫و‬َ
‫لى‬َ ‫إ‬ِ ‫نا‬ً ‫س‬َ ‫ح‬َ ‫عا‬ً ‫تا‬َ ‫م‬َ ‫م‬ْ ‫ك‬ُ ‫ع‬ْ ‫ت‬ّ ‫م‬َ ‫ي‬ُ ‫ه‬ِ ‫ي‬ْ ‫ل‬َ ‫إ‬ِ
‫ذي‬ِ ‫ل‬ّ ‫ك‬ُ ‫ت‬ِ ‫ؤ‬ْ ‫ي‬ُ ‫و‬َ ‫مى‬ّ ‫س‬َ ‫م‬ُ ‫ل‬ٍ ‫ج‬َ ‫أ‬َ
‫ني‬ّ ‫إ‬ِ ‫ف‬َ ‫وا‬ْ ‫ل‬ّ ‫و‬َ ‫ت‬َ ‫ن‬ْ ‫إ‬ِ ‫و‬َ ‫ه‬ُ ‫ل‬َ ‫ض‬ْ ‫ف‬َ ‫ل‬ٍ ‫ض‬ْ ‫ف‬َ
‫ر‬ٍ ‫بي‬ِ ‫ك‬َ ‫م‬ٍ ‫و‬ْ ‫ي‬َ ‫ب‬َ ‫ذا‬َ ‫ع‬َ ‫م‬ْ ‫ك‬ُ ‫ي‬ْ ‫ل‬َ ‫ع‬َ ‫ف‬ُ ‫خا‬َ ‫أ‬َ
[ ]‫هــود‬
http://mfarra.cst.ps
OO
OO
PP
QUESTIONS?QUESTIONS?
http://mfarra.cst.ps
Thank You …Thank You …

More Related Content

What's hot (20)

PPTX
Chapter2 array of objects
Mahmoud Alfarra
 
PPTX
Inheritance and Polymorphism Java
M. Raihan
 
PDF
Javapolymorphism
karthikenlume
 
PPT
Polymorphism in java, method overloading and method overriding
JavaTportal
 
PPTX
oops concept in java | object oriented programming in java
CPD INDIA
 
PPTX
Inheritance
Sapna Sharma
 
PPTX
Pi j3.2 polymorphism
mcollison
 
PPTX
Dynamic method dispatch
yugandhar vadlamudi
 
PPTX
Lecture 8 abstract class and interface
manish kumar
 
PPT
JAVA Polymorphism
Mahi Mca
 
PPTX
Multiple inheritance possible in Java
Kurapati Vishwak
 
DOCX
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
PPTX
Polymorphism
prabhat kumar
 
PPSX
Seminar on java
shathika
 
PPTX
Data members and member functions
Marlom46
 
PPTX
Classes, objects in JAVA
Abhilash Nair
 
PPT
Lect 1-class and object
Fajar Baskoro
 
PPTX
Class and object
prabhat kumar
 
PPTX
Class or Object
Rahul Bathri
 
PPT
Class and object in C++
rprajat007
 
Chapter2 array of objects
Mahmoud Alfarra
 
Inheritance and Polymorphism Java
M. Raihan
 
Javapolymorphism
karthikenlume
 
Polymorphism in java, method overloading and method overriding
JavaTportal
 
oops concept in java | object oriented programming in java
CPD INDIA
 
Inheritance
Sapna Sharma
 
Pi j3.2 polymorphism
mcollison
 
Dynamic method dispatch
yugandhar vadlamudi
 
Lecture 8 abstract class and interface
manish kumar
 
JAVA Polymorphism
Mahi Mca
 
Multiple inheritance possible in Java
Kurapati Vishwak
 
JAVA Notes - All major concepts covered with examples
Sunil Kumar Gunasekaran
 
Polymorphism
prabhat kumar
 
Seminar on java
shathika
 
Data members and member functions
Marlom46
 
Classes, objects in JAVA
Abhilash Nair
 
Lect 1-class and object
Fajar Baskoro
 
Class and object
prabhat kumar
 
Class or Object
Rahul Bathri
 
Class and object in C++
rprajat007
 

Viewers also liked (20)

PPTX
15 نصيحة للطالب الجامعي الجديد
Mahmoud Alfarra
 
PPT
graph based cluster labeling using GHSOM
Mahmoud Alfarra
 
PPTX
ثلاث خطوات عملية للطالب الجامعي قبل الامتحان
Mahmoud Alfarra
 
PPT
البرمجة الهدفية بلغة جافا - مقدمة
Mahmoud Alfarra
 
PPTX
Object Oriented Programming in Java _lecture 1
Mahmoud Alfarra
 
PPTX
Graphs data Structure
Mahmoud Alfarra
 
PPTX
Document clustering and classification
Mahmoud Alfarra
 
PPTX
Citrix Fit4Cloud Reihe: Citrix XenServer in der Cloud
Digicomp Academy AG
 
PDF
Unit2 review
shoetzlein
 
PDF
ООО БАРРЕЛЬ НЕФТЬ ГРУПП - презентационный каталог компании
Дмитрий Выскорко
 
PPT
fdhgfd
maialengenua
 
PPT
Kewirausahaan
heldamayasari
 
PPSX
Anger in the Light of J Krishnamurti Teachings
Saumitra Das
 
PPTX
He Has Such Quiet Eyes
Po Po Tun
 
PDF
Critical purviews where information and communication technology (ICT) can pr...
Adamkolo Mohammed Ibrahim Jinjiri
 
PPTX
Show me your hands
Terry Penney
 
PDF
Cob 20081113 1
macavity_d_katt
 
DOC
ルイ·ヴィトンの荷物を購入
luhan366
 
PDF
Top 10 Tips from Millionaires
maemis
 
PDF
Gazeta
jornaldaquimoc
 
15 نصيحة للطالب الجامعي الجديد
Mahmoud Alfarra
 
graph based cluster labeling using GHSOM
Mahmoud Alfarra
 
ثلاث خطوات عملية للطالب الجامعي قبل الامتحان
Mahmoud Alfarra
 
البرمجة الهدفية بلغة جافا - مقدمة
Mahmoud Alfarra
 
Object Oriented Programming in Java _lecture 1
Mahmoud Alfarra
 
Graphs data Structure
Mahmoud Alfarra
 
Document clustering and classification
Mahmoud Alfarra
 
Citrix Fit4Cloud Reihe: Citrix XenServer in der Cloud
Digicomp Academy AG
 
Unit2 review
shoetzlein
 
ООО БАРРЕЛЬ НЕФТЬ ГРУПП - презентационный каталог компании
Дмитрий Выскорко
 
fdhgfd
maialengenua
 
Kewirausahaan
heldamayasari
 
Anger in the Light of J Krishnamurti Teachings
Saumitra Das
 
He Has Such Quiet Eyes
Po Po Tun
 
Critical purviews where information and communication technology (ICT) can pr...
Adamkolo Mohammed Ibrahim Jinjiri
 
Show me your hands
Terry Penney
 
Cob 20081113 1
macavity_d_katt
 
ルイ·ヴィトンの荷物を購入
luhan366
 
Top 10 Tips from Millionaires
maemis
 
Ad

Similar to البرمجة الهدفية بلغة جافا - الوراثة (20)

PPTX
Chapter 8.2
sotlsoc
 
PPT
RajLec10.ppt
Rassjb
 
PPTX
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
PPTX
Inheritance Slides
Ahsan Raja
 
PDF
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
PPT
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
argsstring
 
PDF
OOPs Concepts - Android Programming
Purvik Rana
 
PPTX
Chap3 inheritance
raksharao
 
DOCX
Class notes(week 6) on inheritance and multiple inheritance
Kuntal Bhowmick
 
PPTX
Unit3 part2-inheritance
DevaKumari Vijay
 
PDF
Java Inheritance
Rosie Jane Enomar
 
PPT
Inheritance & Polymorphism - 1
PRN USM
 
PPTX
Pi j3.1 inheritance
mcollison
 
PPT
M251_Meeting 5 (Inheritance and Polymorphism).ppt
smartashammari
 
PPTX
UNIT 5.pptx
CurativeServiceDivis
 
PPT
7_-_Inheritance
Krishna Sujeer
 
PPT
Java inheritance
GaneshKumarKanthiah
 
PPTX
Ch5 inheritance
HarshithaAllu
 
PPTX
Inheritance ppt
Nivegeetha
 
Chapter 8.2
sotlsoc
 
RajLec10.ppt
Rassjb
 
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Inheritance Slides
Ahsan Raja
 
‏‏‏‏‏‏oop lecture objectives will come.pdf
nabeehmohammedtaher
 
Java for the Impatient, Java Constructors, Scope, Wrappers, Inheritance, and ...
argsstring
 
OOPs Concepts - Android Programming
Purvik Rana
 
Chap3 inheritance
raksharao
 
Class notes(week 6) on inheritance and multiple inheritance
Kuntal Bhowmick
 
Unit3 part2-inheritance
DevaKumari Vijay
 
Java Inheritance
Rosie Jane Enomar
 
Inheritance & Polymorphism - 1
PRN USM
 
Pi j3.1 inheritance
mcollison
 
M251_Meeting 5 (Inheritance and Polymorphism).ppt
smartashammari
 
7_-_Inheritance
Krishna Sujeer
 
Java inheritance
GaneshKumarKanthiah
 
Ch5 inheritance
HarshithaAllu
 
Inheritance ppt
Nivegeetha
 
Ad

More from Mahmoud Alfarra (20)

PPT
Computer Programming, Loops using Java - part 2
Mahmoud Alfarra
 
PPT
Computer Programming, Loops using Java
Mahmoud Alfarra
 
PPT
Chapter 10: hashing data structure
Mahmoud Alfarra
 
PPT
Chapter9 graph data structure
Mahmoud Alfarra
 
PPT
Chapter 8: tree data structure
Mahmoud Alfarra
 
PPT
Chapter 7: Queue data structure
Mahmoud Alfarra
 
PPT
Chapter 6: stack data structure
Mahmoud Alfarra
 
PPT
Chapter 5: linked list data structure
Mahmoud Alfarra
 
PPT
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
PPT
Chapter 3: basic sorting algorithms data structure
Mahmoud Alfarra
 
PPT
Chapter 2: array and array list data structure
Mahmoud Alfarra
 
PPT
Chapter1 intro toprincipleofc#_datastructure_b_cs
Mahmoud Alfarra
 
PPT
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
PPTX
3 classification
Mahmoud Alfarra
 
PPT
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 
PPT
7 programming-using-java decision-making220102011
Mahmoud Alfarra
 
PPT
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
PPT
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
PPT
4 programming-using-java intro-tojava20102011
Mahmoud Alfarra
 
PPT
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 
Computer Programming, Loops using Java - part 2
Mahmoud Alfarra
 
Computer Programming, Loops using Java
Mahmoud Alfarra
 
Chapter 10: hashing data structure
Mahmoud Alfarra
 
Chapter9 graph data structure
Mahmoud Alfarra
 
Chapter 8: tree data structure
Mahmoud Alfarra
 
Chapter 7: Queue data structure
Mahmoud Alfarra
 
Chapter 6: stack data structure
Mahmoud Alfarra
 
Chapter 5: linked list data structure
Mahmoud Alfarra
 
Chapter 4: basic search algorithms data structure
Mahmoud Alfarra
 
Chapter 3: basic sorting algorithms data structure
Mahmoud Alfarra
 
Chapter 2: array and array list data structure
Mahmoud Alfarra
 
Chapter1 intro toprincipleofc#_datastructure_b_cs
Mahmoud Alfarra
 
Chapter 0: introduction to data structure
Mahmoud Alfarra
 
3 classification
Mahmoud Alfarra
 
8 programming-using-java decision-making practices 20102011
Mahmoud Alfarra
 
7 programming-using-java decision-making220102011
Mahmoud Alfarra
 
6 programming-using-java decision-making20102011-
Mahmoud Alfarra
 
5 programming-using-java intro-tooop20102011
Mahmoud Alfarra
 
4 programming-using-java intro-tojava20102011
Mahmoud Alfarra
 
3 programming-using-java introduction-to computer
Mahmoud Alfarra
 

Recently uploaded (20)

PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
PDF
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
PPTX
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
PPTX
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
PDF
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PPTX
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PDF
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
PDF
Horarios de distribución de agua en julio
pegazohn1978
 
PPTX
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
PPTX
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
How to Configure Re-Ordering From Portal in Odoo 18 Website
Celine George
 
Characteristics, Strengths and Weaknesses of Quantitative Research.pdf
Thelma Villaflores
 
TRANSLATIONAL AND ROTATIONAL MOTION.pptx
KIPAIZAGABAWA1
 
Introduction to Biochemistry & Cellular Foundations.pptx
marvinnbustamante1
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
STATEMENT-BY-THE-HON.-MINISTER-FOR-HEALTH-ON-THE-COVID-19-OUTBREAK-AT-UG_revi...
nservice241
 
QNL June Edition hosted by Pragya the official Quiz Club of the University of...
Pragya - UEM Kolkata Quiz Club
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
How to Send Email From Odoo 18 Website - Odoo Slides
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
Week 2 - Irish Natural Heritage Powerpoint.pdf
swainealan
 
Horarios de distribución de agua en julio
pegazohn1978
 
DAY 1_QUARTER1 ENGLISH 5 WEEK- PRESENTATION.pptx
BanyMacalintal
 
care of patient with elimination needs.pptx
Rekhanjali Gupta
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 

البرمجة الهدفية بلغة جافا - الوراثة

  • 1. O O P Inheritance Object Oriented Programming Prepared & Presented by: Mahmoud Rafeek Alfarra 2012 Chapter 3
  • 2. OO OO PP http://mfarra.cst.ps Contents What is Inheritance ? Advantages1 Superclasses and Subclasses2 "is-a" and the "has-a" relationship3 Mini Example: Vehicle4 Constructors in Subclasses5 Example: Students’ types6 Single Vs Multiple inheritance7 Important Notes8
  • 3. OO OO PP What is Inheritance ? Inheritance is a form of software reuse in which a new class is created by absorbing an existing class's members and embellishing them with new or modified capabilities. http://mfarra.cst.ps P_Properties P_ methods P_Properties P_ methods Parent Class C_Properties C_methods C_Properties C_methods Child Class This class has its properties, methods and that of its parent. This class has its properties, methods and that of its parent.
  • 4. OO OO PP Advantages of Inheritance http://mfarra.cst.ps Using inheritance:  to minimize the amount of duplicate code.  A better organization of code and smaller, simpler compilation units.  make application code more flexible to change because classes that inherit from a common superclass can be used interchangeably.
  • 5. OO OO PP Superclasses and Subclasses http://mfarra.cst.ps P_Properties P_ methods P_Properties P_ methods SuperClass C_Properties C_methods C_Properties C_methods SubClass  When creating a class, rather than declaring completely new members, the programmer can designate that the new class should inherit the members of an existing class.  The existing class is called the superclass, and the new class is the subclass.  Each subclass can become the superclass for future subclasses.
  • 6. OO OO PP Superclasses and Subclasses http://mfarra.cst.ps A subclass normally adds its own fields and methods. Therefore, a subclass is more specific than its superclass. Typically, the subclass exhibits the behaviors of its superclass and additional behaviors that are specific to the subclass. In Java, the class hierarchy begins with class Object (in package java.lang), which every class in Java directly or indirectly extends.
  • 7. OO OO PP properties3 methods3 properties3 methods3 SubClass Direct & Indirect Superclasses http://mfarra.cst.ps The direct superclass is the superclass from which the subclass explicitly inherits.  An indirect superclass is any class above the direct superclass in the class hierarchy. properties1 methods1 properties1 methods1 Indirect SuperClass properties2 methods2 properties2 methods2 Direct SuperClass
  • 8. OO OO PP "is-a" and the "has-a" relationship http://mfarra.cst.ps "Is-a" represents inheritance. In an "is-a" relationship, an object of a subclass can also be treated as an object of its superclass. For example, a car is a vehicle. Vehicle Class Vehicle Class SuperClass Car ClassCar Class SubClass Honda is a car & Honda is a vehicle Honda is a car & Honda is a vehicle Object of sub is also an object of superObject of sub is also an object of super
  • 9. OO OO PP Mini Example: VehicleProject http://mfarra.cst.ps class Vehicle { protected String model; protected float price; public Vehicle(String model, float price){ this.model = model; this.price = price; } public String print(){ return "Model: "+model+"t Price: "+price; } public void setModel(String model){ this.model = model; } public String getModel(){ return model; } // set and get of price }
  • 10. OO OO PP Mini Example: Car class http://mfarra.cst.ps class Car extends Vehicle{ private int passengers; public Car(String model, float price,int passengers){ super( model, price); this.passengers = passengers; } public String print(){ return "Data is:n"+super.print()+ " # of passengers: "+passengers; } } A compilation error occurs if a subclass constructor calls one of its superclass constructors with arguments that do not match the superclass constructor declarations.
  • 11. OO OO PP Mini Example: using sub & super class http://mfarra.cst.ps public class VehicleProjectInheritance { public static void main(String[] args) { Car c = new Car("Honda", 455.0f, 4); System.out.println(c.print()); } }
  • 12. OO OO PP Constructors in Subclasses  When a program creates a subclass object, the subclass constructor immediately calls the superclass constructor.  The superclass constructor's body executes to initialize the superclass's instance variables that are part of the subclass object, then the subclass constructor's body executes to initialize the subclass-only instance variables. http://mfarra.cst.ps
  • 13. OO OO PP Constructors in Subclasses http://mfarra.cst.ps Public Test1(){ } Public Test1(){ } SuperClass Public Test2(){ } Public Test2(){ } SubClass calls Return values Subclass constructor invokes its direct superclass's constructor either explicitly (via the super reference) or implicitly (calling the superclass's default constructor or no- argument constructor).
  • 15. OO OO PP Example: Student Class http://mfarra.cst.ps class Student { protected String name; protected String mobile; protected float gpa; public Student(String name, String mobile, float gpa){ this.name = name; this.mobile = mobile; this.gpa = gpa; } // set methods public void setName(String name){ this.name= name; } … // get methods public String getName(){ return name; } …. // Print Data public String showData(){ return "Name: "+getName()+"nMobile: "+getMobile()+"nGPA: "+getGpa(); } }
  • 16. OO OO PP Example: PostGraduated Class http://mfarra.cst.ps class PostGraduated extends Student { private int hours; public PostGraduated(String name, String mobile, float gpa,int hours){ super(name, mobile, gpa); this.hours = hours; } // set method public void setHours(int hours){ this.hours = hours; } // get method public int getHours(){ return hours; } public String PrintData (){ return showData() + " Hours: "+getHours(); } } Declare another method to calculate the earn of post graduate if the cost of each hour is 20$.
  • 17. OO OO PP Example: Graduate Class http://mfarra.cst.ps class Graduate extends Student { private String trainingField; public Graduate(String name, String mobile, float gpa, String trainingField){ super(name, mobile, gpa); this.trainingField = trainingField; } public void setTrainingField(String trainingField){ this.trainingField= trainingField; } public String setTrainingField(){ return trainingField; } public String Printinfo(){ return showData() + " TrainingField: "+getTrainingField(); } } Declare another method to calculate the grade of the graduate student (Excellent, V.Good, …)
  • 18. OO OO PP Method Overridden http://mfarra.cst.ps An instance method in a subclasssubclass with the same signature (name, plus the number and the type of its parameters) and return type as an instance method in the superclasssuperclass overrides the superclass's method. public class Animal { public static void testClassMethod() { System.out.println("The class" + " method in Animal"); } public void testInstanceMethod() { System.out.println("The instance " + " method in Animal."); } } public class Cat extends Animal { public static void testClassMethod() { System.out.println("The class method" + " in Cat."); } public void testInstanceMethod() { System.out.println("The instance method" + " in Cat."); } Overrideby Overrideby
  • 19. OO OO PP Method Overridden http://mfarra.cst.ps public int calcsal(int x){ sal = days*x; return sal; } public int calcsal(int x){ sal = days*x; return sal; } public int calcsal(int x){ sal = (days*x)- absent; return sal; } public int calcsal(int x){ sal = (days*x)- absent; return sal; } Super obj1 = new Super (); int x = obj. Super obj1 = new Super (); int x = obj. Calcsal(20);Calcsal(20); Super class Sub class Sub obj2 = new Super (); int x = obj. Sub obj2 = new Super (); int x = obj. Calcsal(20);Calcsal(20);
  • 20. OO OO PP Method Overridden Vs Method Overloading http://mfarra.cst.ps Using code, distinguish between overridden and overloading method. Implement the concept of method overridden on the methods of print in example of Students’ types
  • 21. OO OO PP Example: Inherited members from Student Class http://mfarra.cst.ps Inherited method from student class Inherited method from student class
  • 22. OO OO PP Single Vs Multiple inheritance Class 1Class 1 Class 2Class 2 Class 3Class 3 Class 1Class 1 Class 2Class 2 Multiple Inheritance Single Inheritance Class can inherit members from more than one class (more than one super class) Class can inherit members from only one class (one super class) Java was designed without multiple inheritance. Java was designed without multiple inheritance. Java was designed with single inheritance. Java was designed with single inheritance.
  • 23. OO OO PP Important Notes Methods of a subclass cannot directly access private members of their superclass. With inheritance, the common instance variables and methods of all the classes in the hierarchy are declared in a superclass. Use the protected access modifier when a superclass should provide a method only to its subclasses and other classes in the same package, but not to other clients. A subclass is more specific than its superclass and represents a smaller group of objects. A superclass's protected members have an intermediate level of protection between public and private access. They can be accessed by members of the superclass, by members of its subclasses and by members of other classes in the same package.
  • 24. OO OO PP ‫ربكم‬ ‫استغفروا‬ :‫ذكره‬ ‫تعالى‬ ‫ا‬ ‫قال‬ ‫بوا‬ُ ‫تو‬ُ ‫م‬ّ ‫ث‬ُ ‫م‬ْ ‫ك‬ُ ‫ب‬ّ ‫ر‬َ ‫روا‬ُ ‫ف‬ِ ‫غ‬ْ ‫ت‬َ ‫س‬ْ ‫ا‬ ‫ن‬ِ ‫أ‬َ ‫و‬َ ‫لى‬َ ‫إ‬ِ ‫نا‬ً ‫س‬َ ‫ح‬َ ‫عا‬ً ‫تا‬َ ‫م‬َ ‫م‬ْ ‫ك‬ُ ‫ع‬ْ ‫ت‬ّ ‫م‬َ ‫ي‬ُ ‫ه‬ِ ‫ي‬ْ ‫ل‬َ ‫إ‬ِ ‫ذي‬ِ ‫ل‬ّ ‫ك‬ُ ‫ت‬ِ ‫ؤ‬ْ ‫ي‬ُ ‫و‬َ ‫مى‬ّ ‫س‬َ ‫م‬ُ ‫ل‬ٍ ‫ج‬َ ‫أ‬َ ‫ني‬ّ ‫إ‬ِ ‫ف‬َ ‫وا‬ْ ‫ل‬ّ ‫و‬َ ‫ت‬َ ‫ن‬ْ ‫إ‬ِ ‫و‬َ ‫ه‬ُ ‫ل‬َ ‫ض‬ْ ‫ف‬َ ‫ل‬ٍ ‫ض‬ْ ‫ف‬َ ‫ر‬ٍ ‫بي‬ِ ‫ك‬َ ‫م‬ٍ ‫و‬ْ ‫ي‬َ ‫ب‬َ ‫ذا‬َ ‫ع‬َ ‫م‬ْ ‫ك‬ُ ‫ي‬ْ ‫ل‬َ ‫ع‬َ ‫ف‬ُ ‫خا‬َ ‫أ‬َ [ ]‫هــود‬ http://mfarra.cst.ps