Inheritance, Packages, and Interfaces Flashcards

1
Q

Does a superclass have access to the members of a subclass? Does a subclass have access to the members of a superclass?

A

No, a superclass has no knowledge of its subclasses. Yes, a subclass has access to all nonprivate members of its superclass.

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

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.

A

// A subclass of TwoDShape for circles.
class Circle extends TwoDShape {
// A default constructor.
Circle() {
super();
}

// Construct Circle
Circle(double x) {
super(x, “circle”); // call superclass constructor
}

// Construct an object from an object.
Circle(Circle ob) {
super(ob); // pass object to TwoDShape constructor
}

double area() {
return (getWidth() / 2) * (getWidth() / 2) * 3.1416;
}
}

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

How do you prevent a subclass from having access to a member of a superclass?

A

To prevent a subclass from having access to a superclass member, declare that member as private.

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

Describe the purpose and use of the two versions of super described in this chapter.

A

The super keyword has two forms. The first is used to call a superclass constructor. The general form
of this usage is

super (param-list);

The second form of super is used to access a superclass member. It has this general form:

super.member

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

Given the following hierarchy, in what order do the constructors for these classes complete their execution when a Gamma object is instantiated?

class Alpha { …

class Beta extends Alpha { …

Class Gamma extends Beta { …

A

Constructors complete their execution in order of derivation. Thus, when a Gamma object is created,
the order is Alpha, Beta, Gamma.

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

A superclass reference can refer to a subclass object. Explain why this is important as it is related to method overriding.

A

When an overridden method is called through a superclass reference, it is the type of the object being
referred to that determines which version of the method is called.

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

What is an abstract class?

A

An abstract class contains at least one abstract method.

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

How do you prevent a method from being overridden? How do you prevent a class from being inherited?

A

To prevent a method from being overridden, declare it as final. To prevent a class from being inherited,
declare it as final.

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

Explain how inheritance, method overriding, and abstract classes are used to support polymorphism.

A

Inheritance, method overriding, and abstract classes support polymorphism by enabling you to create
a generalized class structure that can be implemented by a variety of classes. Thus, the abstract class
defines a consistent interface that is shared by all implementing classes. This embodies the concept of
“one interface, multiple methods.”

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

What class is a superclass of every other class?

A

The Object class.

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

A class that contains at least one abstract method must, itself, be declared abstract. True or False?

A

True.

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

What keyword is used to create a named constant?

A

final

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

Assume that class B inherits class A. Further, assume a method called makeObj( ) that is declared as shown here:

A makeObj(int which ) {
if(which == 0) return new A();
else return new B();
}

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?

A

var myRef = makeObj(1);

Even though a B object is created, the type of myRef will be A because that is the declared return type of makeObj( ). When using local variable type inference, the inferred type of a variable is based on the declared type of its initializer. Therefore, if the initializer is of a superclass type (which is A in this case), that will be the type of the variable. It does not matter if the actual object being referred to by the initializer is an instance of a derived class.

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

Assuming the situation described in Question 13, what will the type of myRef be given thisstatement?

A

var myRef = (B) makeObj(1);

In this case, the cast to B specifies the type of the initializer, and myRef is of type B.

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

Using the code from Try This 8-1, put the ICharQ interface and its three implementations
into a package called qpack. Keeping the queue demonstration class IQDemo in the default
package, show how to import and use the classes in qpack.

A

To put ICharQ and its implementations into the qpack package, you must separate each into its own
file, make each implementation class public, and add this statement to the top of each file.

package qpack;

Once this has been done, you can use qpack by adding this import statement to IQDemo.

import qpack.*;

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

What is a namespace? Why is it important that Java allows you to partition the namespace?

A

A namespace is a declarative region. By partitioning the namespace, you can prevent name collisions.

17
Q

Typically, packages are stored in __________.

A

directories

18
Q

Explain the difference between protected and default access.

A

A member with protected access can be used within its package and by a subclass in other packages.

A member with default access can be used only within its package.

19
Q

Explain the two ways that the members of a package can be used by other packages.

A

To use a member of a package, you can either fully qualify its name, or you can import it using import.

20
Q

“One interface, multiple methods” is a key tenet of Java. What feature best exemplifies it?

A

The interface best exemplifies the one interface, multiple methods principle of OOP.

21
Q

How many classes can implement an interface? How many interfaces can a class implement?

A

An interface can be implemented by an unlimited number of classes. A class can implement as many
interfaces as it chooses.

22
Q

Can interfaces be extended?

A

Yes, interfaces can be extended.

23
Q

Create an interface for the Vehicle class from Chapter 7. Call the interface IVehicle.

A

interface IVehicle {

// Return the range.
int range();

// Compute fuel needed for a given distance.
double fuelneeded(int miles);

// Access methods for instance variables.
int getPassengers();
void setPassengers(int p);
int getFuelcap();
void setFuelcap(int f);
int getMpg();
void setMpg(int m);
}

24
Q

Variables declared in an interface are implicitly static and final. Can they be shared with other parts of a program?

A

Yes, interface variables can be used as named constants that are shared by all files in a program.
They are brought into view by implementing their interface.

25
Q

A package is, in essence, a container for classes. True or False?

A

True.

26
Q

What standard Java package is automatically imported into a program?

A

java.lang

27
Q

What keyword is used to declare a default interface method?

A

default

28
Q

Is it possible to define a static method in an interface?

A

Yes

29
Q

Assume that the ICharQ interface shown in Try This 8-1 has been in widespread use for several years. Now, you want to add a method to it called reset(), which will be used to reset the queue to its empty, starting condition. How can this be accomplished without
breaking preexisting code?

A

To avoid breaking preexisting code, you must use a default interface method. Because you can’t
know how to reset each queue implementation, the default reset() implementation will need to report
an error that indicates that it is not implemented. (The best way to do this is to use an exception.
Exceptions are examined in the following chapter.) Fortunately, since no preexisting code assumes
that ICharQ defines a reset() method, no preexisting code will encounter that error, and no
preexisting code will be broken.

30
Q

How is a static method in an interface called?

A

A static interface method is called through its interface name, by use of the dot operator.