Monday, 19 October 2009

SCJP Sun Certified Programmer By Kathy Sierra and Bert Bates - Part2

Couple of weeks back i started practicing SCJP Sun Certified Programmer for Java 5 Study Guide By Kathy Sierra and Bert Bates.

Great book written by Kathy Sierra and Bert Bates.

I wanted to share few important quotations found from the second chapter.

1) The rules for overriding a method are as follows:

a) The argument list must exactly match that of the overridden method. If they don't match, we can end up with an overloaded method.

b) The return type must be the same as, or a subtype of, the return type declared in the original overridden method in the superclass.

c) The access level can't be more restrictive than the overridden method's.

d) The access level CAN be less restrictive than that of the overridden method.

e) Instance methods can be overridden only if they are inherited by the subclass. A subclass within the same package as the instance's superclass can override any superclass method that is not marked private or final. A subclass in a different package can override only those non-final methods marked public or protected (since protected methods are inherited by the subclass).

f) The overriding method CAN throw any unchecked (runtime) exception, regardless of whether the overridden method declares the exception.

g) The overriding method must NOT throw checked exceptions that are new or broader 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.

h) 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.

i) You cannot override a method marked final or static.

2) If a method is overridden but you use a polymorphic (supertype) reference to refer to the subtype object with the overriding method, the compiler assumes you're calling the supertype version of the method. If the supertype version declares a checked exception, but the overriding subtype method does not, the compiler still thinks you are calling a method that declares an exception.


class Animal {
public void eat() throws Exception {
// throws an Exception
}
}
class Dog2 extends Animal {
public void eat() { // no Exceptions }
public static void main(String [] arga) {
Animal a = new Dog2();
Dog2 d = new Dog2();
d.eat(); // ok
a.eat(); // compiler error -
// unreported exception
}
}



3) Overridden version of the 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 of the argument passed at compile time.

4) Below table summarizes the difference between overloaded and overridden methods







Overloaded MethodOverridden Method
Argument(s)Can change.Can't change except for covariant returns.
ExceptionsCan change.Can reduce or climinate. Must not throw new or broader checked exceptions.
AccessCan change.Must not make more restrictive (can be less restrictive).
InvocationReference type determines which overloaded version (based on declared argument types) is selected. Happens at compile time. The actual method that's invoked is still a virtual method invocation that happens at runtime, but the compiler will already know the signature of the method to be invoked. So at runtime, the argument match will already have been nailed down, just not the class in which the method lives.Object type (in other words, the type of the actual instance on the heap) determines which method is selected. Happens at runtime.


5) Only static variables and methods can be accessed as part of the call to super() or this(). (Example: super (Animal.NAME) is OK, because NAME is declared as a static variable.)

6) Static Methods Can't Be Overridden.

7) Coupling is the degree to which one class knows about another class.

8) The term cohesion is used to indicate the degree to which a class has a single, well-focused purpose. Keep in mind that cohesion is a subjective concept. The more focused a class is, the higher its cohesiveness—a good thing. The key benefit of high cohesion is that such classes are typically much easier to maintain (and less frequently changed) than classes with low cohesion. Another benefit of high cohesion is that classes with a well-focused purpose tend to be more reusable than other classes.

9) Polymorphism applies to overriding, not to overloading.

10) Reference type determines which overloaded method will be used at compile time.

No comments: