Professional Documents
Culture Documents
members
1. Does a superclass have access to the members of a subclass? Does a subclass have access to the
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.s statuze
3. How do you prevent a subclass from having access to a member of asuperclass?o
g i t ontt d
4. Describe the purpose and use of both versions of super.
5. Given the following hierarchy:6oetdo watoflod
274 Java Fundamentals
class Alpha...
class Beta extends Alpha{ ..
21. Suppose you are writinga Java program that does a lot of mathematical calculations involving . If youonly
need 2 decimal places of accuracy, you could use 3.14 everywhere ris needed in your code (for example, in
formulas such as area = 3.14*r*r), or you could declarea constant PI equal to 3.14 and then use PI
instead of 3.14everywhere z is needed (and so use formulas such as area= PI*r*r),
A. Show what the declaration of such a constant PI would look like in Java.
B. Give at least two reasons why using the declared constant PI is better than using 3.14.
22. One of the uses of inheritance is to eliminate code duplication. For example, suppose you have two classes
Aand B and both have identical getData) methods that extract data from files, but both classes do different
things with the data once they've extracted it. Describe one way that inheritance can be used to avoid the
Ke duplicate code.oHCe
23. Suppose aclass Adeclares an equals( ) method that takes aparameter of type A. Such aclassthe
willinherited
also in
herit the equals() method from the Object class. Does the equals() method in class A override
equals( ) method, or is the equals() method overloaded in class A?