Learning Object Oriented Language Features

1. Implementation inheritance is inheritance through class extension.

2. Java supports implementation inheritance by providing reserved word extends.

3. A subclass can have only one superclass because Java does not support multiple implementation inheritance.

4. You prevent a class from being subclassed by declaring the class final.

5. The answer is false: the super() call can only appear in a constructor.

6. If a superclass declares a constructor with one or more parameters, and if a subclass constructor does not use super() to call that constructor, the compiler reports an error because the subclass constructor attempts to call a nonexistent noargument constructor in the superclass.

7. An immutable class is a class whose instances cannot be modified.

8. The answer is false: a class cannot inherit constructors.

9. Overriding a method means to replace an inherited method with another method that provides the same signature and the same return type, but provides a new implementation.

10. To call a superclass method from its overriding subclass method, prefix the superclass method name with reserved word super and the member access operator in the method call.

11. You prevent a method from being overridden by declaring the method final.

12. You cannot make an overriding subclass method less accessible than the superclass method it is overriding because subtype polymorphism would not work properly if subclass methods could be made less accessible.

Suppose you upcast a subclass instance to superclass type by assigning the instance's reference to a variable of superclass type. Now suppose you specify a superclass method call on the variable. If this method is overridden by the subclass, the subclass version of the method is called. However, if access to the subclass's overriding method's access could be made private, calling this method would break encapsulation—private methods cannot be called directly from outside of their class.

13. You tell the compiler that a method overrides another method by prefixing the overriding method's header with the @Override annotation.

14. Java does not support multiple implementation inheritance because this form of inheritance can lead to ambiguities.

15. The name of Java's ultimate superclass is Object. This class is located in the java.lang package.

16. The purpose of the clone() method is to duplicate an object without calling a constructor.

17. Object's clone() method throws CloneNotSupportedException when the class whose instance is to be shallowly cloned does not implement the Cloneable interface.

18. The difference between shallow copying and deep copying is that shallow copying copies each primitive or reference field's value to its counterpart in the clone, whereas deep copying creates, for each reference field, a new object and assigns its reference to the field. This deep copying process continues recursively for these newly created objects.

19. The == operator cannot be used to determine if two objects are logically equivalent because this operator only compares object references, not the contents of these objects.

20. Object's equals() method compares the current object's this reference to the reference passed as an argument to this method. (When I refer to Object's equals() method, I am referring to the equals() method in the Object class.)

21. Expression "abc" == "a" + "bc" returns true. It does so because the String class contains special support that allows literal strings and string-valued constant expressions to be compared via ==.

22. You can optimize a time-consuming equals() method by first using == to determine if this method's reference argument identifies the current object (which is represented in source code via reserved word this).

23. The purpose of the finalize() method is to provide a safety net for calling an object's cleanup method in case that method is not called.

24. You should not rely on finalize() for closing open files because file descriptors are a limited resource and an application might not be able to open additional files until finalize() is called, and this method might be called infrequently (or perhaps not at all).

25. A hash code is a small value that results from applying a mathematical function to a potentially large amount of data.

26. The answer is true: you should override the hashCode() method whenever you override the equals() method.

27. Object's toString() method returns a string representation of the current object that consists of the object's class name, followed by the @ symbol, followed by a hexadecimal representation of the object's hash code. (When I refer to Object's toString() method, I am referring to the toString() method in the Object class.)

28. You should override toString() to provide a concise but meaningful description of the object in order to facilitate debugging via System.out.println() method calls. It is more informative for toString() to reveal object state than to reveal a class name, followed by the @ symbol, followed by a hexadecimal representation of the object's hash code.

29. Composition is a way to reuse code by composing classes out of other classes, based upon a has-a relationship between them.

30. The answer is false: composition is used to implement "has-a" relationships and implementation inheritance is used to implement "is-a" relationships.

31. The fundamental problem of implementation inheritance is that it breaks encapsulation. You fix this problem by ensuring that you have control over the superclass as well as its subclasses, by ensuring that the superclass is designed and documented for extension, or by using a wrapper class in lieu of a subclass when you would otherwise extend the superclass.

32. Subtype polymorphism is a kind of polymorphism where a subtype instance appears in a supertype context, and executing a supertype operation on the subtype instance results in the subtype's version of that operation executing.

33. Subtype polymorphism is accomplished by upcasting the subtype instance to its supertype, by assigning the instance's reference to a variable of that type, and, via this variable, calling a superclass method that has been overridden in the subclass.

34. You would use abstract classes and abstract methods to describe generic concepts (such as shape, animal, or vehicle) and generic operations (such as drawing a generic shape). Abstract classes cannot be instantiated and abstract methods cannot be called because they have no code bodies.

35. An abstract class can contain concrete methods.

36. The purpose of downcasting is to access subtype features. For example, you would downcast a Point variable that contains a Circle instance reference to the Circle type so that you can call Circle's getRadius() method on the instance.

37. The three forms of RTTI are the virtual machine verifying that a cast is legal, using the instanceof operator to determine if an instance is a member of a type, and reflection.

38. A covariant return type is a method return type that, in the superclass's method declaration, is the supertype of the return type in the subclass's overriding method declaration.

39. You formally declare an interface by specifying at least reserved word interface, followed by a name, followed by a brace-delimited body of constants and/or method headers.

40. The answer is true: you can precede an interface declaration with the abstract reserved word.

41. A marker interface is an interface that declares no members.

42. Interface inheritance is inheritance through interface implementation or interface extension.

43. You implement an interface by appending an implements clause, consisting of reserved word implements followed by the interface's name, to a class header, and by overriding the interface's method headers in the class.

44. You might encounter one or more name collisions when you implement multiple interfaces.

45. You form a hierarchy of interfaces by appending reserved word extends followed by an interface name to an interface header.

46. Java's interfaces feature is so important because it gives developers the utmost flexibility in designing their applications.

47. Interfaces and abstract classes describe abstract types.

48. Interfaces and abstract classes differ in that interfaces can only declare abstract methods and constants, and can be implemented by any class in any class hierarchy. In contrast, abstract classes can declare constants and nonconstant fields, can declare abstract and concrete methods, and can only appear in the upper levels of class hierarchies, where they are used to describe abstract concepts and behaviors.

49. Listings 4 through 10 declare the Animal, Bird, Fish, AmericanRobin, DomesticCanary, RainbowTrout, and SockeyeSalmon classes that were called for in Chapter 3.

Listing 4. The Animal class abstracting over birds and fish (and other organisms)

public abstract class Animal {

private String kind; private String appearance;

public Animal(String kind, String appearance) {

this.kind = kind; this.appearance = appearance;

public abstract void eat(); public abstract void move(); ^Override public final String toString() {

Listing 5. The Bird class abstracting over American robins, domestic canaries, and other kinds of birds public abstract class Bird extends Animal {

public Bird(String kind, String appearance) {

super(kind, appearance);

^Override public final void eat() {

System.out.println("eats seeds and insects");

^Override public final void move() {

System.out.println("flies through the air");

Listing 6. The Fish class abstracting over rainbow trout, sockeye salmon, and other kinds of fish public abstract class Fish extends Animal {

public Fish(String kind, String appearance) {

super(kind, appearance);

^Override public final void eat() {

System.out.println("eats krill, algae, and insects");

^Override public final void move() {

System.out.println("swims through the water");

Listing 7. The AmericanRobin class denoting a bird with a red breast public final class AmericanRobin extends Bird {

public AmericanRobin() {

super("americanrobin", "red breast");

Listing 8. The DomesticCanary class denoting a bird of various colors public final class DomesticCanary extends Bird {

public DomesticCanary() {

super("domestic canary", "yellow, orange, black, brown, white, red");

Listing 9. The RainbowTrout class denoting a rainbow-colored fish public final class RainbowTrout extends Fish {

public RainbowTrout() {

super("rainbowtrout", "bands of brilliant speckled multicolored " + "stripes running nearly the whole length of its body");

Listing 10. The SockeyeSalmon class denoting a red-and-green fish public final class SockeyeSalmon extends Fish {

public SockeyeSalmon() {

super("sockeyesalmon", "bright red with a green head");

Animal's toString() method is declared final because it does not make sense to override this method, which is complete in this example. Also, each of Bird's and Fish's overriding eat() and move() methods is declared final because it does not make sense to override these methods in this example, which assumes that all birds eat seeds and insects; all fish eat krill, algae, and insects; all birds fly through the air; and all fish swim through the water.

The AmericanRobin, DomesticCanary, RainbowTrout, and SockeyeSalmon classes are declared final because they represent the bottom of the Bird and Fish class hierarchies, and it does not make sense to subclass them.

50. Listing 11 declares the Animals class that was called for in Chapter 3. Listing 11. The Animals class letting animals eat and move public class Animals {

public static void main(String[] args) {

Animal[] animals = { new AmericanRobin(), new RainbowTrout(), new DomesticCanary(), new SockeyeSalmon() };

System.out.println(animals[i]); animals[i].eat(); animals[i].move(); System.out.println();

51. Listings 12 through 14 declare the Countable interface, the modified Animal class, and the modified Animals class that were called for in Chapter 3.

Listing 12. The Countable interface for use in taking a census of animals public interface Countable {

String getID();

Listing 13. The refactored Animal class for help in census taking public abstract class Animal implements Countable {

private String kind; private String appearance;

public Animal(String kind, String appearance) {

this.kind = kind; this.appearance = appearance;

public abstract void eat(); public abstract void move(); ^Override public final String toString() {

^Override public final String getID() {

return kind;

Listing 14. The modified Animals class for carrying out the census public class Animals {

public static void main(String[] args) {

Animal[] animals = { new AmericanRobin(), new RainbowTrout(), new DomesticCanary(), new SockeyeSalmon(), new RainbowTrout(), new AmericanRobin() };

System.out.println(animals[i]); animals[i].eat(); animals[i].move(); System.out.println();

Census census = new Census();

Countable[] countables = (Countable[]) animals;

census.update(countables[i].getID());

for (int i = 0; i < Census.SIZE; i++) System.out.println(census.get(i));

Was this article helpful?

0 0

Post a comment