Java Questions 31 - 40  «Prev  Next»

Java Questions 32

  1. What does it mean that the overriding method must NOT throw checked exceptions that are new or broader than those declared by the overridden method?

    Answer:
    For example, a method that declares a FileNotFoundException cannot be overridden by a method that declares a SQLException, Exception, or any other non-runtime exception unless it is a subclass of FileNotFoundException.

  2. What does overriding imply with respect to inheritance?

    Answer:
    Overriding implies that you are re-implementing a method you inherited.
  3. Why can't the keyword super be used with static methods?

    Answer:
    Using super to invoke an overridden method only applies to instance methods because static methods cannot be overridden. Instance methods are non-static methods.

  4. How do you define method overloading in Java?

    Answer:
    Overloaded methods let you reuse the same method name in a class, but with different arguments ( and optionally, a different return type).

  5. Can two objects which are not equal have the same hashCode?

    Answer:
    Yes. Two objects, which are not equal by means of the equals() method can still return the same hashCode.

  6. How can you remember that a double is larger than a float?

    Answer:
    When you think of double, think of double the number of bits as a float data type.
    double = 64 bits , float =32 bits

  7. When it comes to method overloading, at what time is it determined to call a method ?

    Answer:
    The choice of which overloaded method to call (in other words, the signature of the method) is NOT dynamically decided at runtime but at compile time.

  8. What determines which overloaded method is called?

    Answer:
    When overloading methods, the reference type determines which overloaded method is invoked.

  9. What is the main difference between method overloading and method overriding?

    Answer:
    Which overridden version of the method to call ( from the class in the inheritance tree) is decided at runtime based on the object type.
    Which overloaded version of the method to call is based on the reference type of the argument passed at compile time.

  10. What is an important distinction between overloaded and overridden methods?

    Answer:
    1. Polymorphism does not determine which overloaded version is called.
    2. Polymorphism does come into play when the decision is made with regards to which overridden version of a method is called.