Object Oriented Programming_Lecture 3
Let’s think on fields of class
Visibility Modifiers and Accessor Methods
الكلية الجامعية للعلوم والتكنولوجيا - خان يونس
University college of science & technology
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
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.