You are on page 1of 3

EXERCISES

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{ ..

& Class Gamma extends


In what order are the constructors for these classes executed when a Gamma object is instantiated?
6. A superclass reference can refer to a subclass object. Explain why this is important ás it relates
overriding.
7. What is an abstract class?
8. How do youprevent a method from being overridden? How do you prevent a class from being inheried
9. Explain how inheritance, method
overriding, and abstract classes are used to support polymorphisn.
10. What class is a superclass of every other class?
11. Aclass 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?ooi a9
13. Suppose aclass Ahas methods ml(), m2(), and m3() and asubclass Bhas methods m4() and ms0)
A. Which of the five methods are accessible by objects of class A?
B. Which of the five methods are accessible by objects of class B?
C. Which of the five methods can be called using avariable of type Athat refersto an object of type B
D. Suppose m1() is declared private. Which of the five methods are now accessible by objects of clas B
14. Create a Person class with private instance variables for the person's name and birth date. Add appropi:
ate accessor methods for these variables. Then create a subclass CollegeGraduate with private instancg
variables for the student's GPA and year of graduation and appropriate accessors for these variables. Don
forget to include appropriate constructors for your classes. Then create a class with a main( ) method thut
demonstrates your classes.
15. Standard practice in many situations is to declare all nonfinal instance variables to be private and then pre
vide appropriate accessor methods for accessing the variables. For some instance variables it is approprialte
to have a"getter" method for getting the value of the variable but no "setter" method for changing the val1e
of the variable. Think of an example of a class and an instance variable where this situation occurs and er
plain why it is not appropriate to have a "'setter" method for the variable.
16. In Chapter 6,. aFailSoftArray class was created to prevent boundary errors from occurring when the aray was
taccessed. However, the class is nottotally fail-soft due to the fact that the length instance variable is public.fyou
were to purposely misuse the class by assigning lengtha value greater than the size of theunderlying array, anat
tempt to access the array beyond its bounds would not be prevented. What should be done to the length variableu
prevent this type of misuse from occurring? Remember: length needs to remain public so that users can
determine the length of the array.
17. Override the inherited toString() method of the FailSoftArray class from Chapter 6 so that it returns astue
displaying the contents of the array. For example, if the array contains 1.2,3, and4,then the toString() methd
should return the string "{ 1, 2, 3,4}". (For more information about overriding toString( ), see Chaple
18. Suppose we were to enhance our modeling of vehicles by creating Tire objects to represent the tires o t
hicle. Howshould the Tire class be related to the Vehicle class? Should it be a subclass of Vehicle?
owhy not?
19. Suppose you have classes Boat, House, HouseBoat, and BoatHouse for representing boats, houses, house-
boats (a floating house), and boathouses (a building for storing boats), respectively. Should any of the classes
be subclasses of any of the other classes? Why or why not?
20. Is the following code segment legal in Java? Why or why not?
int [] array =1,2,3};
Object [] data = "hello", new Object (), array}i
Inheritance 275

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?

You might also like