Lecture 3 - Encapsulation
Lecture 3 - Encapsulation
Encapsulation
Encapsulation
The meaning of Encapsulation, is to make
sure that "sensitive" data is hidden from
users. To achieve this, you must:
– declare class variables/attributes as private (only
accessible within the same class)
– provide public setter and getter methods to access
and update the value of a private variable
Benefits of Encapsulation
• The shielding of the data means that when
the format of the private data need to
change for some reason, the objects
invoking methods on the object whose data
has changed will not need to change the
format they send their messages in.
Benefits of Encapsulation
• For example, if we imagine that we needed
to change a data attribute from a type float
to a type double.
Benefits of Encapsulation
• If a client object is updating the value directly
then by changing the data type you will also
need to change the code in the client object. If
you are dealing with large systems with
hundreds of classes this type of situation can
quickly get out of hand with several layers of
"knock-on" changes required. Therefore,
encapsulation promotes maintenance because
code changes can be made independently
without effecting other classes.
Benefits of Encapsulation
• From a software development perspective
by having well defined public services it
allows other developers to quickly
understand your code and reuse it in other
applications.
Benefits of Encapsulation
• In summary the benefits of encapsulation
are:
– Encapsulation promotes maintenance
– Code changes can be made independently
– Increases usability
Benefits of Encapsulation
• And encapsulation is implemented with:
– Public interfaces
– controls of visibility of operations & state
– data is private / not accessible
Java Access Modifiers
• A Java access modifier specifies which classes can
access a given class and its fields, constructors and
methods. Access modifiers can be specified separately
for a class, its constructors, fields and methods. Java
access modifiers are also sometimes referred to in daily
speech as Java access specifiers, but the correct name
is Java access modifiers. Classes, fields, constructors
and methods can have one of four different Java access
modifiers:
– private
– default (package)
– protected
Java Access Modifiers
• The following table summarizes what Java
constructs each Java access modifier can be
applied to:
Constructor ✓ ✓ ✓ ✓
Method ✓ ✓ ✓ ✓
Field ✓ ✓ ✓ ✓
private Access Modifiers
• If a method or variable is marked as private (has
the private access modifier assigned to it), then
only code inside the same class can access the
variable, or call the method. Code inside
subclasses cannot access the variable or method,
nor can code from any external class.
private Access Modifiers
• Classes cannot be marked with the private access
modifier. Marking a class with the private access
modifier would mean that no other class could
access it, which means that you could not really
use the class at all. Therefore the private access
modifier is not allowed for classes.
private Access Modifiers
public class Clock {
private long time = 0;
}
• The member variable time has been marked
as private. That means, that the member
variable time inside the Clock class cannot be
accessed from code outside the Clock class.
default (package) Access Modifiers
• The default Java access modifier is declared by
not writing any access modifier at all. The default
access modifier means that code inside the class
itself as well as code inside classes in the same
package as this class, can access the class, field,
constructor or method which the default access
modifier is assigned to. Therefore,
the default access modifier is also sometimes
referred to as the package access modifier.
protected Access Modifiers
• The protected access modifier provides the same
access as the default access modifier, with the
addition that subclasses can
access protected methods and member variables
(fields) of the superclass. This is true even if the
subclass is not located in the same package as
the superclass.
public Access Modifiers
• The Java access modifier public means that all
code can access the class, field, constructor or
method, regardless of where the accessing code
is located. The accessing code can be in a
different class and different package