SlideShare a Scribd company logo
1 of 31
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

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 

What's hot (20)

Pi j3.2 polymorphism
Pi j3.2 polymorphismPi j3.2 polymorphism
Pi j3.2 polymorphism
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
Class or Object
Class or ObjectClass or Object
Class or Object
 
Object and class
Object and classObject and class
Object and class
 
Lect 1-class and object
Lect 1-class and objectLect 1-class and object
Lect 1-class and object
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
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...
 
Oops in java
Oops in javaOops in java
Oops in java
 
Classes&objects
Classes&objectsClasses&objects
Classes&objects
 
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
 
11 Using classes and objects
11 Using classes and objects11 Using classes and objects
11 Using classes and objects
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
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
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
Class and Objects in Java
Class and Objects in JavaClass and Objects in Java
Class and Objects in Java
 
Java Methods
Java MethodsJava Methods
Java Methods
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 

Similar to ‫Object Oriented Programming_Lecture 3

Similar to ‫Object Oriented Programming_Lecture 3 (20)

Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
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
 
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...
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
8 polymorphism
8 polymorphism8 polymorphism
8 polymorphism
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
Java basics
Java basicsJava basics
Java basics
 
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
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Lecture 5.pptx
Lecture 5.pptxLecture 5.pptx
Lecture 5.pptx
 
CJP Unit-1 contd.pptx
CJP Unit-1 contd.pptxCJP Unit-1 contd.pptx
CJP Unit-1 contd.pptx
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
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
 
Java
JavaJava
Java
 
UNIT - IIInew.pptx
UNIT - IIInew.pptxUNIT - IIInew.pptx
UNIT - IIInew.pptx
 
02 java basics
02 java basics02 java basics
02 java basics
 
Hemajava
HemajavaHemajava
Hemajava
 

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

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

Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
KarakKing
 

Recently uploaded (20)

Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

‫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