SlideShare a Scribd company logo
Object Oriented Programming
Chapter 1: Introduction to OOP
Prepared by: Mahmoud Rafeek Alfarra
2016
Outlines
◉ What is Object-Oriented Programming ?
◉ Procedural vs. Object-Oriented Programming
◉ OO Programming Concepts
◉ Concept of Objects and classes
◉ UML Class Diagram
◉ Visibility Modifiers and Accessor Methods
◉ Full Example
(َ‫ه‬ْ‫ي‬َ‫ل‬َ‫ع‬َ‫ف‬ َ‫ء‬‫َا‬‫س‬َ‫أ‬ ْ‫ن‬َ‫م‬َ‫و‬ ِ‫ه‬ِ‫س‬ْ‫ف‬َ‫ن‬ِ‫ل‬َ‫ف‬ ‫ِحًا‬‫ل‬‫َا‬‫ص‬ َ‫ل‬ِ‫م‬َ‫ع‬ ْ‫ن‬َ‫م‬‫َى‬‫ل‬ِ‫إ‬ ََّ‫م‬ُ‫ث‬ ‫ا‬
َ‫ن‬‫ُو‬‫ع‬َ‫ج‬ْ‫ر‬ُ‫ت‬ ْ‫م‬ُ‫ك‬َِّ‫ب‬َ‫ر‬)
Lecture
Let’s think on fieldes of class
3
Sample class
class Pencil {
public String color = “red”;
public int length;
public float diameter;
public static long nextID = 0;
public void setColor (String newColor) {
color = newColor;
}
}
o Java provides a number of access modifiers to help you set the
level of access you want for classes as well as the fields,
methods and constructors in your classes.
Access control modifiers
A member has package or
default accessibility when no
accessibility modifier is
specified.
A standard design strategy is to
make all fields private and
provide public getter methods
for them.
o private: private members are accessible only in the class itself.
o package: package members are accessible in classes in the same
package and the class itself.
o protected: protected members are accessible in classes in the
same package, in subclasses of the class, and in the class itself.
o public: public members are accessible anywhere the class is
accessible.
Access control modifiers
Access control modifiers
public class Pencil {
public String color = “red”;
public int length;
public float diameter;
private float price;
public static long nextID = 0;
public void setPrice (float
newPrice) {
price = newPrice;
} }
public class CreatePencil {
public static void main (String args[]){
Pencil p1 = new Pencil();
p1.price = 0.5f;
} }
Pencil.java
CreatePencil.java
o final
 once initialized, the value cannot be changed.
 often be used to define named constants.
 static final fields must be initialized when the class is
initialized.
 non-static final fields must be initialized when an object of
the class is constructed.
More Access control modifiers
Methods
Overloading
ConstructorParameter
Values
main
method
o main method
o The system locates and runs the main method for a class
when you run a program
o Other methods get execution when called by the main
method explicitly or implicitly.
o Must be public, static and void.
main method
o A class can have more than one method with the same name as
long as they have different parameter list.
public class Pencil {
. . .
public void setPrice (float newPrice) {
price = newPrice;
}
public void setPrice (Pencil p) {
price = p.getPrice();
}
}
Overloading
o Parameters are always passed by value.
public void method1 (int a) {
a = 6;
}
public void method2 ( ) {
int b = 3;
method1(b); // now b = ?
// b = 3
}
Parameter Values
o When the parameter is an object reference, it is the object
reference, not the object itself, getting passed.
Parameter Values
public void method1 (Car a) {
…
}
class PassRef{
public static void main(String[] args) {
Pencil plainPencil = new Pencil("PLAIN");
System.out.println("original color: " +
plainPencil.color);
paintRed(plainPencil);
System.out.println("new color: " +
plainPencil.color);
}
public static void paintRed(Pencil p) {
p.color = "RED";
p = null;
}
}
Parameter is an object reference
color: PLAIN
color: PLAIN
color: RED
color: RED NULL
p
plainPencil
plainPencil
plainPencil p
plainPencil p
o If you change any field of the object which the parameter
refers to, the object is changed for every variable which holds a
reference to this object.
o You can change which object a parameter refers to inside a
method without affecting the original reference which is passed.
o What is passed is the object reference, and it’s passed in the
manner of “PASSING BY VALUE”!
Parameter is an object reference
o Constructors are a special kind of methods that are invoked to
construct objects.
Constructors
Circle() {
}
Circle(double newRadius) {
radius = newRadius;
}
o A constructor with no parameters is referred to as a no-arg
constructor.
o Constructors must have the same name as the class itself.
o Constructors do not have a return type—not even void.
o Constructors are invoked using the new operator when an object
is created.
o Constructors play the role of initializing objects.
Constructors
o A class may be defined without constructors. In this case, a no-
arg constructor with an empty body is implicitly defined in the
class.
o This constructor, called a default constructor, is provided
automatically only if no constructors are explicitly defined in the
class.
Constructors
Copying Variables of Object Types
i
Primitive type assignment i = j
Before:
1
j 2
i
After:
2
j 2
c1
Object type assignment c1 = c2
Before:
c2
c1
After:
c2
c1: Circle
radius = 5
C2: Circle
radius = 9
c1: Circle
radius = 5
C2: Circle
radius = 9
o As shown in the previous figure, after the assignment statement
c1 = c2, c1 points to the same object referenced by c2.
o The object previously referenced by c1 is no longer referenced.
o This object is known as garbage.
o Garbage is automatically collected by JVM.
Garbage Collection
The JVM will automatically collect the space of null object.
Variables
Static
Variables
class
variablesConstants
Instance
Variables
=
Instance Vs Static
o Static
o Variables are shared by all the
instances of the class.
o public static int x;
o Methods are not tied to a specific
object.
o public static void sum (int a, int b)
{… }
o Instance
o Variables belong to a specific
instance.
o public int x;
o Methods are invoked by an
instance of the class.
o public void sum (int a, int b)
{… }
Instance Vs Static
o Static constants are final variables shared by all the instances
of the class.
Static constants
o The get and set methods are used to read and modify private
properties.
o The get and set methods are used to read and modify private
properties.
Accessor/Mutator Methods
public void getName( )
{
return name; // as example
}
public void setName(String name)
{
This.name = name; // as example
}
Static constants
o An object cannot access its private members, as shown in (b). It
is OK, however, if the object is declared in its own class, as
shown in (a).
Static constants
Practices
Group 1
Develop 2 Accessor
Methods.
Group 2
Give 3 examples to
use final modefier.
Group 3
Diffrenciate between
Access modefiers.
Group 4
Give 3 examples to
overloaded methods.
Group 5
By drawing, Compare
between Copying
Variables primitives
data types and Object
Types.
Group 6
Develop a simple
class.
THANKS!
Any questions?
You can find me at:
Fb/mahmoudRAlfarra
Staff.cst.ps/mfarra
Youtube.com/mralfarra1
@mralfarra

More Related Content

What's hot (20)

Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
Riaj Uddin Mahi
 
Class or Object
Class or ObjectClass or Object
Class or Object
Rahul Bathri
 
Object and class
Object and classObject and class
Object and class
mohit tripathi
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
Fajar Baskoro
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
Fajar Baskoro
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...
JAINAM KAPADIYA
 
Oops in java
Oops in javaOops in java
Oops in java
baabtra.com - No. 1 supplier of quality freshers
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
M Vishnuvardhan Reddy
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
maznabili
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
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
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Marlom46
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
Java Methods
Java MethodsJava Methods
Java Methods
Rosmina Joy Cabauatan
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
Muthukumaran Subramanian
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 
Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
mcollison
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
Fajar Baskoro
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
rprajat007
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
Fajar Baskoro
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...Method overloading, recursion, passing and returning objects from method, new...
Method overloading, recursion, passing and returning objects from method, new...
JAINAM KAPADIYA
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Tushar B Kute
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
maznabili
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
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
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
Marlom46
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
Spotle.ai
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 

Similar to ‫Object Oriented Programming_Lecture 3 (20)

Lecture 5
Lecture 5Lecture 5
Lecture 5
talha ijaz
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
Epsiba1
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
ssuser8347a1
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
Helen SagayaRaj
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
Sonu WIZIQ
 
8 polymorphism
8 polymorphism8 polymorphism
8 polymorphism
Abhijit Gaikwad
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
foreverredpb
 
Java basics
Java basicsJava basics
Java basics
Shivanshu Purwar
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
AshutoshTrivedi30
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
Mehdi Ali Soltani
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
Java
JavaJava
Java
nirbhayverma8
 
classes-objects in oops java-201023154255.pptx
classes-objects in oops java-201023154255.pptxclasses-objects in oops java-201023154255.pptx
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
akila m
 
02 java basics
02 java basics02 java basics
02 java basics
bsnl007
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
Epsiba1
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
Helen SagayaRaj
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
Sagar Verma
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
foreverredpb
 
Java tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry LevelJava tutorial for Beginners and Entry Level
Java tutorial for Beginners and Entry Level
Ramrao Desai
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
RAJASEKHARV10
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Sagar Verma
 
classes-objects in oops java-201023154255.pptx
classes-objects in oops java-201023154255.pptxclasses-objects in oops java-201023154255.pptx
classes-objects in oops java-201023154255.pptx
janetvidyaanancys
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
akila m
 
02 java basics
02 java basics02 java basics
02 java basics
bsnl007
 

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
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
 
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
 
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
 

Recently uploaded (20)

CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
Sugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptxSugar-Sensing Mechanism in plants....pptx
Sugar-Sensing Mechanism in plants....pptx
Dr. Renu Jangid
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 AccountingHow to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
How to Customize Your Financial Reports & Tax Reports With Odoo 17 Accounting
Celine George
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 

‫Object Oriented Programming_Lecture 3

  • 1. Object Oriented Programming Chapter 1: Introduction to OOP Prepared by: Mahmoud Rafeek Alfarra 2016
  • 2. Outlines ◉ What is Object-Oriented Programming ? ◉ Procedural vs. Object-Oriented Programming ◉ OO Programming Concepts ◉ Concept of Objects and classes ◉ UML Class Diagram ◉ Visibility Modifiers and Accessor Methods ◉ Full Example
  • 3. (َ‫ه‬ْ‫ي‬َ‫ل‬َ‫ع‬َ‫ف‬ َ‫ء‬‫َا‬‫س‬َ‫أ‬ ْ‫ن‬َ‫م‬َ‫و‬ ِ‫ه‬ِ‫س‬ْ‫ف‬َ‫ن‬ِ‫ل‬َ‫ف‬ ‫ِحًا‬‫ل‬‫َا‬‫ص‬ َ‫ل‬ِ‫م‬َ‫ع‬ ْ‫ن‬َ‫م‬‫َى‬‫ل‬ِ‫إ‬ ََّ‫م‬ُ‫ث‬ ‫ا‬ َ‫ن‬‫ُو‬‫ع‬َ‫ج‬ْ‫ر‬ُ‫ت‬ ْ‫م‬ُ‫ك‬َِّ‫ب‬َ‫ر‬)
  • 4. Lecture Let’s think on fieldes of class 3
  • 5. Sample class class Pencil { public String color = “red”; public int length; public float diameter; public static long nextID = 0; public void setColor (String newColor) { color = newColor; } }
  • 6. o Java provides a number of access modifiers to help you set the level of access you want for classes as well as the fields, methods and constructors in your classes. Access control modifiers A member has package or default accessibility when no accessibility modifier is specified. A standard design strategy is to make all fields private and provide public getter methods for them.
  • 7. o private: private members are accessible only in the class itself. o package: package members are accessible in classes in the same package and the class itself. o protected: protected members are accessible in classes in the same package, in subclasses of the class, and in the class itself. o public: public members are accessible anywhere the class is accessible. Access control modifiers
  • 9. public class Pencil { public String color = “red”; public int length; public float diameter; private float price; public static long nextID = 0; public void setPrice (float newPrice) { price = newPrice; } } public class CreatePencil { public static void main (String args[]){ Pencil p1 = new Pencil(); p1.price = 0.5f; } } Pencil.java CreatePencil.java
  • 10. o final  once initialized, the value cannot be changed.  often be used to define named constants.  static final fields must be initialized when the class is initialized.  non-static final fields must be initialized when an object of the class is constructed. More Access control modifiers
  • 12. o main method o The system locates and runs the main method for a class when you run a program o Other methods get execution when called by the main method explicitly or implicitly. o Must be public, static and void. main method
  • 13. o A class can have more than one method with the same name as long as they have different parameter list. public class Pencil { . . . public void setPrice (float newPrice) { price = newPrice; } public void setPrice (Pencil p) { price = p.getPrice(); } } Overloading
  • 14. o Parameters are always passed by value. public void method1 (int a) { a = 6; } public void method2 ( ) { int b = 3; method1(b); // now b = ? // b = 3 } Parameter Values
  • 15. o When the parameter is an object reference, it is the object reference, not the object itself, getting passed. Parameter Values public void method1 (Car a) { … }
  • 16. class PassRef{ public static void main(String[] args) { Pencil plainPencil = new Pencil("PLAIN"); System.out.println("original color: " + plainPencil.color); paintRed(plainPencil); System.out.println("new color: " + plainPencil.color); } public static void paintRed(Pencil p) { p.color = "RED"; p = null; } } Parameter is an object reference color: PLAIN color: PLAIN color: RED color: RED NULL p plainPencil plainPencil plainPencil p plainPencil p
  • 17. o If you change any field of the object which the parameter refers to, the object is changed for every variable which holds a reference to this object. o You can change which object a parameter refers to inside a method without affecting the original reference which is passed. o What is passed is the object reference, and it’s passed in the manner of “PASSING BY VALUE”! Parameter is an object reference
  • 18. o Constructors are a special kind of methods that are invoked to construct objects. Constructors Circle() { } Circle(double newRadius) { radius = newRadius; }
  • 19. o A constructor with no parameters is referred to as a no-arg constructor. o Constructors must have the same name as the class itself. o Constructors do not have a return type—not even void. o Constructors are invoked using the new operator when an object is created. o Constructors play the role of initializing objects. Constructors
  • 20. o A class may be defined without constructors. In this case, a no- arg constructor with an empty body is implicitly defined in the class. o This constructor, called a default constructor, is provided automatically only if no constructors are explicitly defined in the class. Constructors
  • 21. Copying Variables of Object Types i Primitive type assignment i = j Before: 1 j 2 i After: 2 j 2 c1 Object type assignment c1 = c2 Before: c2 c1 After: c2 c1: Circle radius = 5 C2: Circle radius = 9 c1: Circle radius = 5 C2: Circle radius = 9
  • 22. o As shown in the previous figure, after the assignment statement c1 = c2, c1 points to the same object referenced by c2. o The object previously referenced by c1 is no longer referenced. o This object is known as garbage. o Garbage is automatically collected by JVM. Garbage Collection The JVM will automatically collect the space of null object.
  • 24. Instance Vs Static o Static o Variables are shared by all the instances of the class. o public static int x; o Methods are not tied to a specific object. o public static void sum (int a, int b) {… } o Instance o Variables belong to a specific instance. o public int x; o Methods are invoked by an instance of the class. o public void sum (int a, int b) {… }
  • 26. o Static constants are final variables shared by all the instances of the class. Static constants
  • 27. o The get and set methods are used to read and modify private properties. o The get and set methods are used to read and modify private properties. Accessor/Mutator Methods public void getName( ) { return name; // as example } public void setName(String name) { This.name = name; // as example }
  • 29. o An object cannot access its private members, as shown in (b). It is OK, however, if the object is declared in its own class, as shown in (a). Static constants
  • 30. Practices Group 1 Develop 2 Accessor Methods. Group 2 Give 3 examples to use final modefier. Group 3 Diffrenciate between Access modefiers. Group 4 Give 3 examples to overloaded methods. Group 5 By drawing, Compare between Copying Variables primitives data types and Object Types. Group 6 Develop a simple class.
  • 31. THANKS! Any questions? You can find me at: Fb/mahmoudRAlfarra Staff.cst.ps/mfarra Youtube.com/mralfarra1 @mralfarra