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

Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
Mahmoud Alfarra
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
M. Raihan
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
karthikenlume
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
JavaTportal
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
CPD INDIA
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
yugandhar vadlamudi
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
manish kumar
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
Mahi Mca
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
Kurapati Vishwak
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesSunil Kumar Gunasekaran
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
prabhat kumar
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
shathika
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Marlom46
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
Fajar Baskoro
 
Class and object
Class and objectClass and object
Class and object
prabhat kumar
 
Class or Object
Class or ObjectClass or Object
Class or Object
Rahul Bathri
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 

What's hot (20)

Chapter2 array of objects
Chapter2 array of objectsChapter2 array of objects
Chapter2 array of objects
 
Inheritance and Polymorphism Java
Inheritance and Polymorphism JavaInheritance and Polymorphism Java
Inheritance and Polymorphism Java
 
Javapolymorphism
JavapolymorphismJavapolymorphism
Javapolymorphism
 
Polymorphism in java, method overloading and method overriding
Polymorphism in java,  method overloading and method overridingPolymorphism in java,  method overloading and method overriding
Polymorphism in java, method overloading and method overriding
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 
Inheritance
InheritanceInheritance
Inheritance
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
 
JAVA Polymorphism
JAVA PolymorphismJAVA Polymorphism
JAVA Polymorphism
 
Multiple inheritance possible in Java
Multiple inheritance possible in JavaMultiple inheritance possible in Java
Multiple inheritance possible in Java
 
JAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examplesJAVA Notes - All major concepts covered with examples
JAVA Notes - All major concepts covered with examples
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 
Seminar on java
Seminar on javaSeminar on java
Seminar on java
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Class and object
Class and objectClass and object
Class and object
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 

Viewers also liked

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

Viewers also liked (20)

15 نصيحة للطالب الجامعي الجديد
15 نصيحة للطالب الجامعي الجديد15 نصيحة للطالب الجامعي الجديد
15 نصيحة للطالب الجامعي الجديد
 
graph based cluster labeling using GHSOM
graph based cluster labeling using GHSOMgraph based cluster labeling using GHSOM
graph based cluster labeling using GHSOM
 
ثلاث خطوات عملية للطالب الجامعي قبل الامتحان
ثلاث خطوات عملية للطالب الجامعي قبل الامتحانثلاث خطوات عملية للطالب الجامعي قبل الامتحان
ثلاث خطوات عملية للطالب الجامعي قبل الامتحان
 
البرمجة الهدفية بلغة جافا - مقدمة
البرمجة الهدفية بلغة جافا - مقدمةالبرمجة الهدفية بلغة جافا - مقدمة
البرمجة الهدفية بلغة جافا - مقدمة
 
Object Oriented Programming in Java _lecture 1
Object Oriented Programming in Java _lecture 1Object Oriented Programming in Java _lecture 1
Object Oriented Programming in Java _lecture 1
 
Graphs data Structure
Graphs data StructureGraphs data Structure
Graphs data Structure
 
Document clustering and classification
Document clustering and classification Document clustering and classification
Document clustering and classification
 
Citrix Fit4Cloud Reihe: Citrix XenServer in der Cloud
Citrix Fit4Cloud Reihe: Citrix XenServer in der CloudCitrix Fit4Cloud Reihe: Citrix XenServer in der Cloud
Citrix Fit4Cloud Reihe: Citrix XenServer in der Cloud
 
Unit2 review
Unit2 reviewUnit2 review
Unit2 review
 
ООО БАРРЕЛЬ НЕФТЬ ГРУПП - презентационный каталог компании
ООО БАРРЕЛЬ НЕФТЬ ГРУПП - презентационный каталог компании ООО БАРРЕЛЬ НЕФТЬ ГРУПП - презентационный каталог компании
ООО БАРРЕЛЬ НЕФТЬ ГРУПП - презентационный каталог компании
 
fdhgfd
fdhgfdfdhgfd
fdhgfd
 
Kewirausahaan
KewirausahaanKewirausahaan
Kewirausahaan
 
Anger in the Light of J Krishnamurti Teachings
Anger in the Light of J Krishnamurti TeachingsAnger in the Light of J Krishnamurti Teachings
Anger in the Light of J Krishnamurti Teachings
 
He Has Such Quiet Eyes
He Has Such Quiet EyesHe Has Such Quiet Eyes
He Has Such Quiet Eyes
 
Critical purviews where information and communication technology (ICT) can pr...
Critical purviews where information and communication technology (ICT) can pr...Critical purviews where information and communication technology (ICT) can pr...
Critical purviews where information and communication technology (ICT) can pr...
 
Show me your hands
Show me your handsShow me your hands
Show me your hands
 
Cob 20081113 1
Cob 20081113 1Cob 20081113 1
Cob 20081113 1
 
ルイ·ヴィトンの荷物を購入
ルイ·ヴィトンの荷物を購入ルイ·ヴィトンの荷物を購入
ルイ·ヴィトンの荷物を購入
 
Top 10 Tips from Millionaires
Top 10 Tips from MillionairesTop 10 Tips from Millionaires
Top 10 Tips from Millionaires
 
Gazeta
GazetaGazeta
Gazeta
 

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

Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
NithyaN19
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
Victer Paul
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
Rosie Jane Enomar
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
SajidTk2
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
KartikKapgate
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
HarshithaAllu
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
JayMistry91473
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
Maryo Manjaruni
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2Rakesh Madugula
 
Java basics
Java basicsJava basics
Java basics
Shivanshu Purwar
 
14_inheritance.ppt
14_inheritance.ppt14_inheritance.ppt
14_inheritance.ppt
sundas65
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10Terry Yoast
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
Ahsan Raja
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
RudranilDas11
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
Khaled Anaqwa
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
raksharao
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
HimanshuPandey957216
 
LISP: Object Sytstem Lisp
LISP: Object Sytstem LispLISP: Object Sytstem Lisp
LISP: Object Sytstem Lisp
LISP Content
 
LISP:Object System Lisp
LISP:Object System LispLISP:Object System Lisp
LISP:Object System Lisp
DataminingTools Inc
 

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

Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Java - Inheritance Concepts
Java - Inheritance ConceptsJava - Inheritance Concepts
Java - Inheritance Concepts
 
Java Inheritance
Java InheritanceJava Inheritance
Java Inheritance
 
Selenium Training .pptx
Selenium Training .pptxSelenium Training .pptx
Selenium Training .pptx
 
Inheritance and Polymorphism
Inheritance and PolymorphismInheritance and Polymorphism
Inheritance and Polymorphism
 
Ch5 inheritance
Ch5 inheritanceCh5 inheritance
Ch5 inheritance
 
java_inheritance.pdf
java_inheritance.pdfjava_inheritance.pdf
java_inheritance.pdf
 
Jedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented conceptsJedi slides 2.1 object-oriented concepts
Jedi slides 2.1 object-oriented concepts
 
Chap11
Chap11Chap11
Chap11
 
Md02 - Getting Started part-2
Md02 - Getting Started part-2Md02 - Getting Started part-2
Md02 - Getting Started part-2
 
Java basics
Java basicsJava basics
Java basics
 
14_inheritance.ppt
14_inheritance.ppt14_inheritance.ppt
14_inheritance.ppt
 
9781439035665 ppt ch10
9781439035665 ppt ch109781439035665 ppt ch10
9781439035665 ppt ch10
 
Inheritance Slides
Inheritance SlidesInheritance Slides
Inheritance Slides
 
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptxSodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
SodaPDF-converted-inheritanceinjava-120903114217-phpapp02-converted.pptx
 
Android Training (Java Review)
Android Training (Java Review)Android Training (Java Review)
Android Training (Java Review)
 
Chap3 inheritance
Chap3 inheritanceChap3 inheritance
Chap3 inheritance
 
PPT Lecture-1.4.pptx
PPT Lecture-1.4.pptxPPT Lecture-1.4.pptx
PPT Lecture-1.4.pptx
 
LISP: Object Sytstem Lisp
LISP: Object Sytstem LispLISP: Object Sytstem Lisp
LISP: Object Sytstem Lisp
 
LISP:Object System Lisp
LISP:Object System LispLISP:Object System Lisp
LISP:Object System Lisp
 

More from Mahmoud Alfarra

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

More from Mahmoud Alfarra (20)

Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2Computer Programming, Loops using Java - part 2
Computer Programming, Loops using Java - part 2
 
Computer Programming, Loops using Java
Computer Programming, Loops using JavaComputer Programming, Loops using Java
Computer Programming, Loops using Java
 
Chapter 10: hashing data structure
Chapter 10:  hashing data structureChapter 10:  hashing data structure
Chapter 10: hashing data structure
 
Chapter9 graph data structure
Chapter9  graph data structureChapter9  graph data structure
Chapter9 graph data structure
 
Chapter 8: tree data structure
Chapter 8:  tree data structureChapter 8:  tree data structure
Chapter 8: tree data structure
 
Chapter 7: Queue data structure
Chapter 7:  Queue data structureChapter 7:  Queue data structure
Chapter 7: Queue data structure
 
Chapter 6: stack data structure
Chapter 6:  stack data structureChapter 6:  stack data structure
Chapter 6: stack data structure
 
Chapter 5: linked list data structure
Chapter 5: linked list data structureChapter 5: linked list data structure
Chapter 5: linked list data structure
 
Chapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structureChapter 4: basic search algorithms data structure
Chapter 4: basic search algorithms data structure
 
Chapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structureChapter 3: basic sorting algorithms data structure
Chapter 3: basic sorting algorithms data structure
 
Chapter 2: array and array list data structure
Chapter 2: array and array list  data structureChapter 2: array and array list  data structure
Chapter 2: array and array list data structure
 
Chapter1 intro toprincipleofc#_datastructure_b_cs
Chapter1  intro toprincipleofc#_datastructure_b_csChapter1  intro toprincipleofc#_datastructure_b_cs
Chapter1 intro toprincipleofc#_datastructure_b_cs
 
Chapter 0: introduction to data structure
Chapter 0: introduction to data structureChapter 0: introduction to data structure
Chapter 0: introduction to data structure
 
3 classification
3  classification3  classification
3 classification
 
8 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 201020118 programming-using-java decision-making practices 20102011
8 programming-using-java decision-making practices 20102011
 
7 programming-using-java decision-making220102011
7 programming-using-java decision-making2201020117 programming-using-java decision-making220102011
7 programming-using-java decision-making220102011
 
6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-6 programming-using-java decision-making20102011-
6 programming-using-java decision-making20102011-
 
5 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop201020115 programming-using-java intro-tooop20102011
5 programming-using-java intro-tooop20102011
 
4 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava201020114 programming-using-java intro-tojava20102011
4 programming-using-java intro-tojava20102011
 
3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer3 programming-using-java introduction-to computer
3 programming-using-java introduction-to computer
 

Recently uploaded

Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
Balvir Singh
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
MysoreMuleSoftMeetup
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
deeptiverma2406
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
thanhdowork
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
gb193092
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
Wasim Ak
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 

Recently uploaded (20)

Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Operation Blue Star - Saka Neela Tara
Operation Blue Star   -  Saka Neela TaraOperation Blue Star   -  Saka Neela Tara
Operation Blue Star - Saka Neela Tara
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
Mule 4.6 & Java 17 Upgrade | MuleSoft Mysore Meetup #46
 
Best Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDABest Digital Marketing Institute In NOIDA
Best Digital Marketing Institute In NOIDA
 
A Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptxA Survey of Techniques for Maximizing LLM Performance.pptx
A Survey of Techniques for Maximizing LLM Performance.pptx
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Marketing internship report file for MBA
Marketing internship report file for MBAMarketing internship report file for MBA
Marketing internship report file for MBA
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Normal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of LabourNormal Labour/ Stages of Labour/ Mechanism of Labour
Normal Labour/ Stages of Labour/ Mechanism of Labour
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 

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

  • 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).
  • 14. OO OO PP Example: Students’ types http://mfarra.cst.ps • Have part time hours • Have a field training
  • 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