Inheritance, Packages, and Interfaces Flashcards
Does a superclass have access to the members of a subclass? Does a subclass have access to the members of a superclass?
No, a superclass has no knowledge of its subclasses. Yes, a subclass has access to all nonprivate members of its superclass.
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 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 do you prevent a subclass from having access to a member of a superclass?
To prevent a subclass from having access to a superclass member, declare that member as private.
Describe the purpose and use of the two versions of super described in this chapter.
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
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 { …
Constructors complete their execution in order of derivation. Thus, when a Gamma object is created,
the order is Alpha, Beta, Gamma.
A superclass reference can refer to a subclass object. Explain why this is important as it is related to method overriding.
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.
What is an abstract class?
An abstract class contains at least one abstract method.
How do you prevent a method from being overridden? How do you prevent a class from being inherited?
To prevent a method from being overridden, declare it as final. To prevent a class from being inherited,
declare it as final.
Explain how inheritance, method overriding, and abstract classes are used to support polymorphism.
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.”
What class is a superclass of every other class?
The Object class.
A class that contains at least one abstract method must, itself, be declared abstract. True or False?
True.
What keyword is used to create a named constant?
final
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?
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.
Assuming the situation described in Question 13, what will the type of myRef be given thisstatement?
var myRef = (B) makeObj(1);
In this case, the cast to B specifies the type of the initializer, and myRef is of type B.
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.
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.*;