Java Questions 32
- 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.
- What does overriding imply with respect to inheritance?
Answer:
Overriding implies that you are re-implementing a method you inherited.
- 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.
- 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).
- 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.
- 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
- 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.
- What determines which overloaded method is called?
Answer:
When overloading methods, the reference type determines which overloaded method is invoked.
- 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.
- What is an important distinction between overloaded and overridden methods?
Answer:
- Polymorphism does not determine which overloaded version is called.
- Polymorphism does come into play when the decision is made with regards to which overridden version of a method is called.