Object Oriented Design
and Java Programming
Chapter4: Encapsulation
Chapter objectives
● Package concept
● Encapsulation of classes
● Encapsulation of attributes
● Encapsulation of methods
2
Package
In Java, packages are a mechanism for grouping classes to structure
your project files into logical categories.
Packages are organized hierarchically like class directories.
⇒ Package = directory.
3
Package
Project
Package declaration
package1 package package1;
ClassA Import of a package
ClassB
import package2.ClassC; //importing one class
package2 import package2.*; //import all Classes under the
package
ClassC
ClassD
Package21
ClassE 4
Package
Package declaration
Project structure
Import of a package
5
Package
There are also packages that are automatically imported by default for
each Java class.
Ex:
Classes in the "java.lang" package (String, Integer, Boolean,Object ....).
There is a default package in Java, called "default package", which is
used for classes that are not explicitly declared in a package. These
classes can only be accessed by those in the same default package.
6
Encapsulation
Encapsulation is the concept of making the members of an object
members of an object more or less visible to other objects.
Encapsulation is a way of defining a class in such a way that its
attributes cannot be directly manipulated from outside the class,
but only indirectly via its methods.
7
Encapsulation
Encapsulation can be applied to various elements, including :
● Classes: you can control who can access classes.
● Constructors: you can control who can instantiate classes.
● Instance variables: you can control who can access your class's instance
variables.
● Methods: you can control who can call methods in your class.
8
Encapsulation
Java offers 4 access modifiers:
● public : uncontrolled access
● private : limited access to current classes
● protected : access limited to the package member and its
descendants (child classes)
● default : limited access to the package member. There is no
keyword for this type of modifier, so if no public, private or protected
keyword is used, the element is considered to be default.
9
Encapsulation of classes (1/3)
Classes can only be public or default. 10
Encapsulation des classes (2/3)
public
public class A {
…
}
class B { class C extends A { class D {
A a; A a; A a;
… … …
} } }
A public class is visible from any class in the project. 11
Encapsulation des classes (3/3)
default
class A {
…
}
class B { class C extends A { class D {
A a; A a; A a;
… … …
} } }
A default class is visible only to the classes in its package. 12
Encapsulation of attributes (1/4)
public
class A {
public int x;
…
}
class B { class C extends A { class D {
A a=new A(); A a=new A(); A a=new A();
a.x = t ; a.x = t ; a.x = t ;
this.x = t ;
} } }
A public attribute is visible to all classes 13
Encapsulation of attributes (2/4)
private
class A {
private int y;
…
}
class B { class C extends A { class D {
A a=new A(); A a=new A(); A a=new A();
a. y = t; a. y = t; a. y = t;
y= t;
} } }
A private attribute is only accessible from inside the class. 14
Encapsulation of attributes (3/4)
default
class A {
int z;
…
}
class B { class C extends A { class D {
A a=new A(); A a=new A(); A a=new A();
a.z = t; a.z = t; a.z = t;
z= t;
} } }
The default variable can only be accessed from classes in the same package. 15
Encapsulation des attributes (4/4)
protected
class A {
protected int w;
…
}
class B { class C extends A { class D {
A a=new A(); A a=new A(); A a=new A();
a.w = t; a.w = t; a.w = t;
this.w= t;
} } }
A protected attribute is accessible only to classes in the same package and its
subclasses (even if defined in a different package). 16
Encapsulation of methods(1/4)
public
class A {
public void meth1(){ }
…
}
class B { class C extends A { class D {
A a=new A(); A a=new A(); A a=new A();
a.meth1() ; a.meth1() ; a.meth1() ;
this.meth1();
} } }
A public method is visible to all classes 17
Encapsulation of methods(2/4)
private
class A {
private void meth2(){ }
…
}
class B { class C extends A { class D {
A a=new A(); A a=new A(); A a=new A();
a.meth2() ; a.meth2(); a.meth2() ;
meth2();
} } }
A private method is only accessible from inside its class.
18
Encapsulation of methods(3/4)
default
class A {
void meth3(){ }
…
}
class B { class C extends A { class D {
A a=new A(); A a=new A(); A a=new A();
a.meth3() ; a.meth3(); a.meth3() ;
meth3();
} } }
A default method can only be accessed from classes in the same
19
package.
Encapsulation of methods(4/4)
protected
class A {
protected void meth3(){ }
…
}
class B { class C extends A { class D {
A a=new A(); A a=new A(); A a=new A();
a.meth4() ; a.meth4(); a.meth4();
this.meth4();
} } }
A protected method is accessible to classes in the same package and its
subclasses (even if defined in a different package). 20
Encapsulation of methods/attributes
Modifier Class Package Child Class Other class
private visible
visible visible*
default visible
(same package) (same package)
protected visible visible visible
public visible visible visible visible
*Subclasses can access the attributes and methods of the parent class
21
if they are in the same package.
The accessors (GET) and the mutators (SET)
Accessors(getters) and mutators(setters) are special methods for
accessing and modifying an object's attributes.
Accessors are used to read the value of a property, while mutators are
used to change its value.
Attention : Setters and getters must be declared public
22
The accessors (GET) and the mutators (SET)
Accessors are usually defined with Mutators are usually defined with
the keyword "get" followed by the the keyword "set" followed by the
property name, for example : property name, for example :
public class Product { public class Product {
private int quantity; private int quantity;
public void setQuantity(int quantity) {
public int getQuantity() {
this.quantity = quantity;
return quantity; }
} }
}
23
The accessors (GET) and the mutators (SET)
The accessor of a Boolean attribute is written with "is", e.g. :
public class Product {
private boolean onSale;
public boolean isOnSale() {
return onSale;
}
}
24
The accessors (GET) and the mutators (SET)
Using getter and setter allows you to manage modifications
centrally and validate entries before saving them.
public class Product {
private int quantity;
public void setQuantity(int quantity) {
if (quantity < 0)
System.out.println("The quantity must be a positive number");
else
this.quantity = quantity;
}
}
25
Thank you for your attention
26