Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Study guide: Object-oriented language basics, Part 6

Brush up on Java terms, learn tips and cautions, and review homework assignments

  • Print
  • Feedback

Glossary of terms

incorrectly overloaded
When two superclasses each declare methods with signatures that are identical except for their return types, and the compiler, by examining a method call expression, cannot determine which method to call.


interface
The introduction (but not an implementation) of a type at the source code level.


interface inheritance
A form of inheritance where a class inherits all constants and/or method signatures from an interface.


multiple interface inheritance
A form of inheritance where classes inherit the contents of multiple interfaces.


subinterface
An interface that extends another interface.


superinterface
An interface from which other interfaces extend.


tag
To mark.


type
An abstraction that identifies a set of data items, which share some commonality, and a set of operations that manipulate those data items.


Tips and cautions

These tips and cautions will help you write better programs and save you from agonizing over why the compiler produces error messages.

Tips

  • Use an interface when you want to introduce a reference type into a program without fixing that type to a specific implementation.


Cautions

  • Because an interface's method members are implicitly public, you must provide the public keyword in method signatures when implementing an interface's methods in a class. Otherwise, the compiler reports an error. That error occurs because you are trying to narrow a method's visibility, which Java does not permit.
  • Declaring two interfaces with method signatures that have the same name but different return types, and attempting to implement both interfaces in a class, causes the compiler to report an error. That error occurs because the compiler sees that activity as an attempt to incorrectly overload a method.


Homework

This month's homework features two questions:

  1. Why is it unwise to place the NOT_STARTED and STARTED constants, and the isstarted() method in the StartStop interface?
  2. If an interface introduces a type into source code, and if a type consists of a set of data items in addition to a set of methods, where are the interface's data items? (Hint: Constants are not the data items.)


Answers to last month's homework

Below are last month's homework questions and their answers in red:

  1. Why is it not a good idea to add extends Object to a class declaration?
  2. You should not add extends Object to a class declaration because Java does not support multiple implementation inheritance; a class can only extend a single class. For example, if a Car class extends Object, it cannot also extend Vehicle. Besides, all classes inherit from Object, so there is no reason to explicitly extend the Object class.

  3. How would you deeply clone an array of String objects?
  4. You could deeply clone an array of String objects so that corresponding elements in each array reference different String objects with the same contents. First you would perform a shallow clone to size a new array. Then you would use a for loop with appropriate code that assigns to all the new array's elements a new String object with the corresponding old array element's String contents. The following code fragment demonstrates that:

    String [] bigCats =
    {
       "Tiger",
       "Lion",
       "Leopard",
       "Panther",
       "Cougar"
    };
    String [] bigCats2 = (String []) bigCats.clone ();
    for (int i = 0; i < bigCats.length; i++)
         bigCats2 [i] = new String (bigCats [i]);
    
    


  5. Why doesn't the String class allow you to clone String objects? (Hint: Think of immutability.)
  6. The String class does not allow you to clone String objects because Java has a policy where it shares a single String object among multiple references, and cloning violates that policy. The String sharing policy helps reduce a program's memory requirements. For example, suppose you create a String object that contains a sequence of 100 characters. (Note: A String object treats a string as a sequence of characters.) Because each character occupies two bytes (remember Unicode?), 200 bytes of storage dedicate to containing the string. Now, suppose you need an array of 1,000 copies of the object. If each array element references a separate String object containing the same sequence of characters, the program will require 200,000 bytes to hold all the characters in all copies of the string. However, by sharing the single String object, the program only requires 200 characters for a single string, and each element in the array references that same String object. Because of the String sharing policy, strings are considered immutable.



  • Print
  • Feedback

Resources