Lecture 7 Encapsulation, Packages and API
Lecture 7 Encapsulation, Packages and API
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:
Example
public class Person {
// Getter
return name;
// Setter
this.name = newName;
}
Example explained
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.
Example
public class Main {
System.out.println(myObj.name); // error
Run Example »
If the variable was declared as public, we would expect the following output:
John
Instead, we use the getName() and setName() methods to access and update the
variable:
Example
public class Main {
System.out.println(myObj.getName());
// Outputs "John"
Try it Yourself »
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 & 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
The Java API is a library of prewritten classes, that are free to use, included in
the Java Development Environment.
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.
Syntax
import package.name.Class; // Import a single class
Import a Class
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:
Example
import java.util.Scanner;
In the example above, 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:
Example
Using the Scanner class to get user input:
import java.util.Scanner;
class MyClass {
System.out.println("Enter username");
Run Example »
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:
Example
import java.util.*;
Run Example »
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
└── root
└── mypack
└── MyPackageClass.java
MyPackageClass.java
package mypack;
class MyPackageClass {
System.out.println("This is my package!");
Run Example »
The -d keyword specifies the destination for where to save the class file. You
can use any directory name, like c:/user (windows), or, if you want to keep the
package within the same directory, you can use the dot sign ".", like in the
example above.
Note: The package name should be written in lower case to avoid conflict with
class names.
When we compiled the package in the example above, a new folder was
created, called "mypack".
This is my package!