0% found this document useful (0 votes)
19 views31 pages

6 Lecture Six Encapsulation Access Modifiers and Packagespptx 2

Uploaded by

Isaac King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views31 pages

6 Lecture Six Encapsulation Access Modifiers and Packagespptx 2

Uploaded by

Isaac King
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Chapter-6

ACCESS MODIFIERS , ENCAPSULATION and PACKAGES


Access Modifiers
• The access modifiers in Java are used to specify the
scope of the variables, data members, methods, classes
, or constructors. These help to restrict and secure the
access (or, level of access) of the data.
• There are four level of access:
• Visible to the package, the default. No modifiers are needed.
• Visible to the class only (private).
• Visible to the world (public).
• Visible to the package and all subclasses (protected).
Access Modifiers…
• There are four different types of access modifiers in
Java, we have listed them as follows:
• Default (No keyword required)
• Private
• Protected
• Public
Access Modifeirs…
1. Default Access Modifier
• Default access modifier means we do not explicitly declare an
access modifier for a class, field, method, etc.
• A variable or method declared without any access control
modifier is available to any other class in the same package.
• The fields in an interface are implicitly public static final
and the methods in an interface are by default public.
Access Modifiers…
• Example: Default Access Modifier
• Variables and methods can be declared without any modifiers,
as in the following examples:
Access Modifiers…
2. Private Access Modifier
• Methods, variables, and constructors that are declared private
can only be accessed within the declared class itself.
• Private access modifier is the most restrictive access level.
• Class and interfaces cannot be private.
• Variables that are declared private can be accessed outside
the class, if public getter methods are present in the class.
• Using the private modifier is the main way that an object
encapsulates itself and hides data from the outside world.
Access Modifiers…
2. Private Access Modifier: Example1

• Here, the format variable of the Logger class is private, so there's


no way for other classes to retrieve or set its value directly.
• So, to make this variable available to the outside world, we
defined two public methods: getFormat(), which returns the value
of format, and setFormat(String), which sets its value.
Access Modifiers…
2. Private Access Modifier: Example2

• Here, In this example, the data members and class methods of


the Logger class are private.
• Trying to access those class methods in another class will
generate an error. This is demonstrated in the next, Main class,
example.
Access Modifiers…
2. Private Access Modifier: Example2...

• As explain before, the output will generate the error


shown in the figure.
Access Modifiers…
3. Protected Access modifiers
• Variables, methods, and constructors, which are declared
protected in a superclass can be accessed only by the
subclasses in other package or any class within the package of
the protected members' class.
• The protected access modifier cannot be applied to class and
interfaces. Methods, fields can be declared protected, however
methods and fields in an interface cannot be declared
protected.
• Protected access gives the subclass a chance to use the
helper method or variable, while preventing a nonrelated class
from trying to use it.
Access Modifiers…
• Example 1: Protected Access Modifier
• The following parent class uses protected access control, to allow its
child class override openSpeaker() method

Here, if we define openSpeaker()


method as private, then it would not be
accessible from any other class other
than AudioPlayer. If we define it as
public, then it would become
accessible to all the outside world. But
our intention is to expose this method
to its subclass only, that's why we have
used protected modifier.
Access Modifiers…
• Example 2: Protected access modifiers…
Access Modifiers…
4. Public Access Modifier
• A class, method, constructor, interface, etc. declared public
can be accessed from any other class. Therefore, fields,
methods, blocks declared inside a public class can be
accessed from any class belonging to the Java Universe.
• However, if the public class we are trying to access is in a
different package, then the public class still needs to be
imported. Because of class inheritance, all public methods and
variables of a class are inherited by its subclasses.
Access Modifiers…
• Syntax
• The following function uses public access control:
Access Modifiers…
• Example: Public Access Modifier
• This example demonstrates the use of public access
modifier:
Java Access Modifiers and
Inheritance
• The following rules for inherited methods are enforced
• Methods declared public in a superclass also must be public in
all subclasses.
• Methods declared protected in a superclass must either be
protected or public in subclasses; they cannot be private.
• Methods declared private are not inherited at all, so there is
no rule for them.
• The following table shows the summary of the accessibility in
the same/different classes (or, packages) based on the access
modifiers.
Java Access Modifiers and
Inheritance…
• Example
• In this example, we've created a class with a private variable
age and a variable with default scope as name. Using
setter/getter method, we're updating age and getting value
and name is updated directly.
Java Encapsulation
• Encapsulation in Java is a mechanism of wrapping the
data (variables) and code acting on the data (methods)
together as a single unit.
• In encapsulation, the variables of a class will be hidden
from other classes, and can be accessed only through
the methods of their current class. Therefore, it is also
known as data hiding.
Achieving Encapsulation in Java
• As 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
• provide public get and set methods to access and update the
value of a private variable
Get and Set
• You learned from the previous sections that private
variables can only be accessed within the same class
(an outside class has no access to it). However, it is
possible to access them if we provide public get and
set methods.
• The get method returns the variable value, and the set
method sets the value.
• Syntax for both is that they start with either get or set,
followed by the name of the variable, with the first
letter in upper case:
Get and Set…
• Example:
Get and Set…
• From the previous example:
• The get method returns the value of the variable name.
• The set method takes a parameter (newName) and assigns it
to the name variable. The this keyword is used to refer to the
current object.
• However, as the name variable is declared as private, we
cannot access it from outside this class.
• If the variable was declared as public, we would expect the
following output:

• However, as we try to access a private variable, we get an


error:
Get and Set…
• So, in the example, we should instead use the
getName() and setName() methods to access and
update the variable:
Why Encapsulation?
• Better control of class attributes and methods
• Class attributes can be made read-only (if you only use
the get method), or write-only (if you only use the set
method)
• Flexible: the programmer can change one part of the
code without affecting other parts
• Increased security of data
Java Packages and API
• A package in Java is used to group related classes.
• Think of it as a folder in a file directory. We use
packages to avoid name conflicts, and to write a better
maintainable code.
• Packages are divided into two categories:
• Built-in Packages (packages from the Java API)
• User-defined Packages (create your own packages)
Built-in Packages
• The Java API is a library of prewritten classes, that are free
to use, included in the Java Development Environment.
• The library contains components for managing input,
database programming, and much much more. The
complete list can be found at Oracles website:
https://docs.oracle.com/javase/8/docs/api/.
• The library is divided into packages and classes. Meaning
you can either import a single class (along with its methods
and attributes), or a whole package that contain all the
classes that belong to the specified package.
Built-in Packages…
• To use a class or a package from the library, you need to
use the import keyword:
• Syntax:

• Example:
• If you find a class you want to use, for example, the Scanner
class, which is used to get user input, write the following code:
Built-in Packages…
• In the previous example, java.util is a package, while
Scanner is a class of the java.util package.
• To use the Scanner class, create an object of the class
and use any of the available methods found in the
Scanner class documentation.
• In our example, we will use the nextLine() method,
which is used to read a complete line:
• [Look the example in the next slide]
Built-in Packages…
• Using the Scanner class to get user input:
Built-in Packages…
• Import a Package
• There are many packages to choose from. In the previous
example, we used the Scanner class from the java.util
package. This package also contains date and time facilities,
random-number generator and other utility classes.
• To import a whole package, end the sentence with an asterisk
sign (*).
• The following example will import ALL the classes in the
java.util package:
User-defined Packages
• To create your own package, you need to understand that
Java uses a file system directory to store them. Just like
folders on your computer:
• Example:

• To create a package, use the package keyword:

You might also like