Java Questions 31 - 40  «Prev  Next»

Overloading vs. Overriding

  1. Do you have to pay attention to the return type when overloading a method?

    Answer:
    No. When you overload a method (by changing the parameters), the return type does not have to match that of the superclass version.

  2. What is the difference between arguments and parameters?

    Answer:
    Parameters are the elements in the methods signature that indicate what the method must receive when its inovked.

  3. Which characteristics must an overriding method in a subclass contain?

    Answer:
    When a subclass wants to override an inherited method the subclass must define a method that matches the inherited version exactly (For reference types covariant returns are allowed).

  4. What is a covariant return type?

    Answer:
    As of Java 5, you are allowed to change the return type in the overriding method as long as the new return type is a subtype of the declared return type of the overridden method.

  5. What role does the return type play with regards to overloading and overriding?

    Answer:
    Know the following 2 facts:
    1. Overloaded methods can change the return type
    2. overriding methods can change the return type if the overridden method is a subclass of the Superclass.

    Covariant Return Type

  6. For primitive return types, what must be true of the overriding method?

    Answer:
    When overriding a method, the method in the subclass must match the inherited version exactly.

  7. What task must be completed to create a new object?

    Answer:
    You can not make a new object without invoking a constructor.

  8. How are objects initialized?

    Answer:
    Constructors are the code that runs whenever you use the keyword new.
    Initialization: The new operator is followed by a call to a constructor, which initializes the new object.

  9. What must every class possess?

    Answer:
    Every class, including abstract class, must have a constructor.

  10. What are two key points to remember about constructors?

    Answer:
    1. Constructors do not have a return type
    2. Their names must exactly match the class name.