Packages Flashcards

1
Q

A package, in Java, is a named collection of r_________ classes.
A package is really just a f___________.

A

related

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why do we need packages?

To better o__________ and increase r_____________ and r___________
of our code.

A

organize
readability
reusability

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

There are 2 types of packages:

1) b_______- in packages (from the java API)
2) u________ defined packages (created by us)

The Java API is a l____________ 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 contains all the classes that belong to the specified package.

A

built-in
user-defined
library

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

All the core Java packages themselves reside in a global Java directory, named simply java.

This directory is not itself a p_________ but a store for other packages.

A

package

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What do packages contain?

Java packages can contain other p____________ and c__________
files (that is the compiled Java byte code), not source files (the original Java instructions).

A

packages
classes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

The a________ notation allows you to have access to
all class files in the given package.
Note that there can only ever be one ‘.’ in an import statement and the ‘.’ must follow a package name.

Here are some examples of valid and invalid import statements:

import java..; // illegal as contains more than one ‘.
import java.
; // illegal as ‘java’ is not a package
import java.text.*; // fine, allows access to all classes in text package
import javafx.scene.control.Button; // fine, allows access to the
Button class in control package

A

asterisk

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Creating packages

This is a bit weird because instead of creating a package, giving it a name, etc, we simply reference an imaginary package, e.g. hostelApp, along with the keyword ‘package’ at the top of every class that we want to put into that package.

e.g.

package hostelApp; // add this line to the top of the source file

public class Payment
{
// as before
}

What does the keyword ‘package’ do?

A

It instructs the compiler that the class file created from this source file
must be put in a package called hostelApp.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Package Scope

Classes can be made visible outside of their package ONLY if they are declared as p________.

Remember to declare your classes as public when you are using your own package or they will only be visible to other classes in that package.

A

public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When not to declare classes as public

Not all classes in the package need to be declared as public. Some classes may be part of the implementation only and the developer may not wish them to be made
available to the client.
These classes can have p________ s__________ instead of public scope. In this way, packages provide an extra layer of security for your classes.

A

package scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Package Scope

Package scope hides files within the p____________.

To give a class package scope, remove the ‘public’ modifier
e.g. public class Payment –> class payment

Now, when the hostelApp package is imported into another class, this other class has access only to the Hostel class; not to classes like Payment which are hidden in the package with package s_______.

This is demonstrated in the code fragment below:

import hostelApp.*; // this imports only the public classes in
the package
public class SomeOtherClass
{
// the next line will not compile as Payment is hidden in the
hostelApp package

Payment p = new Payment(“January”, 725);
}

A

package
scope

How well did you know this?
1
Not at all
2
3
4
5
Perfectly