Sunday, August 1, 2010

Trick yourself

1. A class have two synchronized methods. Can two different thread access them simultaneously?
A. Yes, provided methods are not static.

2. Why internationalization or localization are named as i18n or l10n?
A. starting with i, then 18 alphabets and ending with n. Its a usage coined at DEC in the 1970s or 80s[1].

3. A super class A and a subclass B have same variable name and getter setter in both the class.
A a = new B();
a.variable;
a.getVariable();
A. a.variable return value from class A and a.getVariable() will call method in class B because its been overridden there but this method should be there in class A as well otherwise it will be an error. In case of overridding, always child is called.

The compiler looks only at the reference type, not the instance type. Polymorphism
lets you use a more abstract supertype (including an interface) reference to refer to
one of its subtypes (including interface implementers).

Still method of super class can be called by calling it using super.

4. Why constructor are overloaded and not overridden?
A. Constructor are with class name and can not be overridden in subclass, can only be overloaded in same class.

5. Overload vs Overridding
A. Overloaded methods and constructors let you use the same method name (or constructor) but with different argument lists. Overriding lets you redefine a method in a subclass, when you need new subclass-specific behavior.

6. Rules to override
A. 1. overriding method cannot have a more restrictive access modifier. If a subclass were allowed to sneak in and change the access modifier on the overriding method, then suddenly at runtime—when the JVM invokes the true
object’s (Horse) version of the method rather than the reference type’s (Animal)
version—the program would die a horrible death. 2. The argument list must exactly match that of the overridden method. 3. The return type must exactly match that of the overridden method. 4. The access level can be less restrictive than that of the overridden method. 5. The overriding method must not throw new or broader checked exceptions than those declared by the overridden method. 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’s a subclass of FileNotFoundException. 6. The overriding method can throw narrower or fewer exceptions. Just because an overridden method “takes risks” doesn’t mean that the overriding subclass’ exception takes the same risks. Bottom line: An overriding method doesn’t have to declare any exceptions that it will never throw, regardless of what the overridden method declares. 7. You cannot override a method marked final.
8. If a method can’t be inherited, you cannot override it.

7. Rules to overload
A. 1. Argument list must change. 2. Return type, access modifier and thrown exceptions can change. 3. A method can be overload in same or any subclass.

Which overridden method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type passed at compile time.

No comments:

Post a Comment