System, but they may not be reproduced for publication



Yüklə 83 Mb.
Pdf görüntüsü
səhifə37/82
tarix19.04.2023
ölçüsü83 Mb.
#106251
1   ...   33   34   35   36   37   38   39   40   ...   82
Java A Beginner’s Guide, Eighth Edition ( PDFDrive )

 Chapter 7 Self Test
1.
Does a superclass have access to the members of a subclass? Does a subclass have
access to the members of a superclass?
2.
Create a subclass of TwoDShape called Circle. Include an area( ) method that
computes the area of the circle and a constructor that uses super to initialize the
TwoDShape portion.
3.
How do you prevent a subclass from having access to a member of a superclass?
4.
Describe the purpose and use of the two versions of super described in this chapter.
5.
Given the following hierarchy:


In what order do the constructors for these classes complete their execution when a
Gamma object is instantiated?
6.
A superclass reference can refer to a subclass object. Explain why this is important as
it relates to method overriding.
7.
What is an abstract class?
8.
How do you prevent a method from being overridden? How do you prevent a class
from being inherited?
9.
Explain how inheritance, method overriding, and abstract classes are used to
support polymorphism.
10.
What class is a superclass of every other class?
11.
A class that contains at least one abstract method must, itself, be declared abstract.
True or False?
12.
What keyword is used to create a named constant?
13.
Assume that class B inherits class A. Further, assume a method called makeObj( )
that is declared as shown here:
Notice that makeObj( ) returns a reference to an object of either type A or B,
depending on the value of which. Notice, however, that the return type of makeObj( )
is A. (Recall that a superclass reference can refer to a subclass object.) Given this
situation and assuming that you are using JDK 10 or later, what is the type of myRef in
the following declaration and why?


14.
Assuming the situation described in Question 13, what will the type of myRef be
given this statement?


Chapter 8
Packages and Interfaces
Key Skills & Concepts

Use packages

Understand how packages affect access

Apply the protected access modifier

Import packages

Know Java’s standard packages
History
Topics
Tutorials
Offers & Deals
Highlights
Settings
Support
Sign Out


T

Understand interface fundamentals

Implement an interface

Apply interface references

Understand interface variables

Extend interfaces

Create default, static, and private interface methods
his chapter examines two of Java’s most innovative features: packages and interfaces.
Packages are groups of related classes. Packages help organize your code and provide
another layer of encapsulation. As you will see in 
Chapter 15
, packages also play an
important role with modules. An interface defines a set of methods that will be
implemented by a class. Thus, an interface gives you a way to specify what a class will
do, but not how it will do it. Packages and interfaces give you greater control over the
organization of your program.
PACKAGES
In programming, it is often helpful to group related pieces of a program together. In
Java, this can be accomplished by using a package. A package serves two purposes.
First, it provides a mechanism by which related pieces of a program can be organized as
a unit. Classes defined within a package must be accessed through their package name.
Thus, a package provides a way to name a collection of classes. Second, a package
participates in Java’s access control mechanism. Classes defined within a package can
be made private to that package and not accessible by code outside the package. Thus,
the package provides a means by which classes can be encapsulated. Let’s examine each
feature a bit more closely.
In general, when you name a class, you are allocating a name from the namespace. A
namespace defines a declarative region. In Java, no two classes can use the same name
from the same namespace. Thus, within a given namespace, each class name must be
unique. The examples shown in the preceding chapters have all used the default
namespace. While this is fine for short sample programs, it becomes a problem as
programs grow and the default namespace becomes crowded. In large programs,
finding unique names for each class can be difficult. Furthermore, you must avoid
name collisions with code created by other programmers working on the same project,


and with Java’s library. The solution to these problems is the package because it gives
you a way to partition the namespace. When a class is defined within a package, the
name of that package is attached to each class, thus avoiding name collisions with other
classes that have the same name, but are in other packages.
Since a package usually contains related classes, Java defines special access rights to
code within a package. In a package, you can define code that is accessible by other
code within the same package but not by code outside the package. This enables you to
create self­contained groups of related classes that keep their operation private.
Defining a Package
All classes in Java belong to some package. When no package statement is specified,
the default package is used. Furthermore, the default package has no name, which
makes the default package transparent. This is why you haven’t had to worry about
packages before now. While the default package is fine for short, sample programs, it is
inadequate for real applications. Most of the time, you will define one or more packages
for your code.
To create a package, put a package command at the top of a Java source file. The
classes declared within that file will then belong to the specified package. Since a
package defines a namespace, the names of the classes that you put into the file become
Yüklə 83 Mb.

Dostları ilə paylaş:
1   ...   33   34   35   36   37   38   39   40   ...   82




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə