Friday, April 13, 2007

CLASSES(6)

CLASSES(6)

Method throws: a throws clause is used to declare any checked exceptions that can result from the execution of a method or constructor.

The notation is: throws ExceptionTypeList
Where ExceptionTypeList can be one or more and the types are either a class type or a type variable.

Note:
1. the type of exception mentioned in a throws clause must be a subtype of Throwable.
2. any checked exception that will be encountered in the body of a method or constructor must be mentioned in a throws clause in the declaration of the method or constructor.
3. if a throws clause does not contain that exception type, then the compiler will fail to handle the exception and this will result in a compile time error.

Two exceptions that are not checked because declaring their every occurrence would be inconvenient are:
a. exceptions that are represented by subclasses of the class Error.
b. Exceptions that are represented by subclasses of the class, RuntimeException.

if a method declaration n in a class or interface B overrides or hides a method declaration m in A where A is a superclass or superinterface of B. if n has a throws clause that mentions any checked exception types, then m must have a throws clause, and for every checked exception type listed in the throws clause of n, that same exception class or one of its supertypes must exist in the erasure of the throws clause of m; otherwise a compile time error will result.

If there exists a throws clause in n such that no throws clause in an unerased m is a supertype of this throws clause, then an unchecked warning must be issued.

Method body: a method body is a block of code that implements the method, or simply a semicolon indicating the lack of an implementation. Note that the body of a method must be a semicolon, ;, if and only if the method is declared abstract or native.

If an implementation is to be provided for a method declared void, but the implementation requires no executable code, the method body should be written as a block that contains no executable code, {}.

A method declared void must not contain a return statement in its code, a method declared with a return type must have a return statement in its body that returns a value of type return type.

A method that specifies a return type could in some cases not return anything.

Inheritance, overriding, hiding: a class C can inherit from its direct superclasses and superinterfaces (a) all non-private methods (whether abstract or not) of the superclass and superinterface that are public, protected or the default (i.e package-private), (b) in the same package as C, and (c) are neither overridden nor hidden by a declaration in the class.

Overriding (by instance methods): the example below illustrates this.
Note:
1. an instance method does not override a static method.

package examples;

public class A {
void m1(int enter){
System.out.println("A enters with "+enter);
}
}

//C is a subclass of A
public class C extends A{

//both methods have the same signatures, zero parameters
void m1(int enter){
System.out.println("C enters with with "+enter);
}

//to access the overridden method, we use super
void overridden(int enter){
super.m1(enter);
}
}

class Loader {

public static void main(String[] args) {
//let's show overriding of instance methods
C thec = new C();
thec.m1(35);

//the printout will show that C.m1 overrides A.m1
//although C inherits A.m1

//to access the overridden method, let's call the
//method that uses super
thec.overridden(56);
}
}

Hiding (by class methods):
Note:
1. a static method does not hide an instance method otherwise a compile time error.

package examples;

public class A {
static void m1(int enter){
System.out.println("A enters with "+enter);
}
}

public class C extends A{
static void m1(int enter){
System.out.println("C enters with "+enter);
}

//accessing the static method, m1 of A
void overridden(int enter){
super.m1(enter);
}
}

class Loader {

public static void main(String[] args) {
//m1 of A is overridden
C.m1(30);

//we can access A.m1 though these ways
//a. using the qualified name
A.m1(60);

//b. calling the super through method overridden
C thec = new C();
thec.overridden(60);
}
}

Requirements in Overriding and Hiding: 2 requirements are:
1. return type substitutability for the overriding method on the overridden
2. the throws clause should be a subtype of the throws in the overridden method.
3. the access modifier on an overriding or hiding method must provide at least as much access as the overridden method, d4
a. if the overridden method is declared public, then the overriding method must be declared public otherwise a compile time error
b. if the overridden method is declared protected, then the overriding method must be declared protected or public.
c. If the overridden method has default access (package-private), then the overriding method cannot be private.

Overloading: overloading occurs when two methods of a class (whether both are declared, or both are inherited, or one is declared and the other inherited) have the same name but signatures that are not override equivalent.
Note that overriding on methods occurs on a signature-by-signature basis

1 comment:

Anonymous said...

What a great web log. I spend hours on the net reading blogs, about tons of various subjects. I have to first of all give praise to whoever created your theme and second of all to you for writing what i can only describe as an fabulous article. I honestly believe there is a skill to writing articles that only very few posses and honestly you got it. The combining of demonstrative and upper-class content is by all odds super rare with the astronomic amount of blogs on the cyberspace.