Friday, April 13, 2007

CLASSES(5)

CLASSES(5)

Method Signature: two methods have the same signature if they have the same name and argument types. Two methods M and N may be said to have the same argument types if the following conditions hold:
- if they have the same number of formal parameters (possibly zero)
- if they have the same number of type parameters (possibly zero)
- if the formal parameters of M are and of N are if it is possible to replace each Bi in N with the As in M and you get a formal parameter list equal to that in M,
then, M and N have the same signatures.

The signature of a method m1 could be said to be the subsignature of a method m2 if either:
- m2 has the same signature as m1
- the signature of m1 is the same as the erasure of the signature of m2.
Note on subsignatures: this feature was incorporated into the java programming language to express a relationship between two signatures although not identical but one can override the other, a good example is allowing a method without generic types to override one with generic types.

it is a compile time error to declare two methods with override-equivalent signatures where override-equivalent signatures operate on two methods m1 and m2 if m1 is a subsignature of m2 or m2 is a subsignature of m1.

Method Modifiers: the method modifiers are:

public protected private abstract static final synchronized native strictfp

notes:
1. the same modifier cannot appear more than once for a method and public protected private have exclusive usage on each other.
2. if a method declaration contains the keyword abstract it cannot contain any of private static final native strictfp or synchronized.
3. if a keyword native is contained in a declaration it cannot contain strictfp any longer.
4. if there is an annotation a on a method declaration that corresponds to an annotation type T, and T has a meta-annotation m such that m corresponds to annotation.Target, then m has an element with value annotation.ElementType.METHOD, or a compile time error results.
5. the other specified above for method modifiers is customary but not a must.

Abstract methods: the abstract modifier introduces a method as a class member, providing its signature, return type and throws clause but providing no implementation for the method. Abstract methods must appear within abstract classes otherwise a compile time error results. An Enum declaration though can contain an abstract method. If an abstract class is to be subclassed, then that subclass must provide an implementation for its abstract methods, otherwise, a compile time error results.

A private method cannot be declared abstract because a private method cannot be implemented from outside the class.

It is an error for a static method to be declared abstract because invocation of such a method through its qualified name will return a compile time error.

It is a compile time error for a final method to be declared abstract because final methods will never allow a subclass to implement this method or any other method because the implementation is finished and perfect.

An abstract class can override another abstract class by providing an abstract method declaration, giving a facility for detailed specification or loosened specification to be attached to that method.

An instance method that is not abstract can be overridden by an abstract method.

Static methods: a method that is declared static is called a class method. A static method cannot be declared abstract. A static method is invoked with reference to the class and not to any particular object. Use of the keywords this or super in referencing a class method will result in compile time error.

On the other hand, a method that is not declared static is called an instance method and is invoked with respect to a particular object, which is the object which the keywords this or super refer to during execution of the method.

Final methods: a method is declared final to prevent subclass overriding or hiding of the method, attempting to do so results in compile time error. A private method and all methods declared within a final class all behave as if they are final because they cannot be overridden.

A final method can never be declared abstract.

Note that final methods on invocation are inlined by the optimizer or machine code generator.

Native methods: a method that is native is implemented in platform-dependent code, typically written in another programming language, typically C, C++, FORTRAN or Assembly language. The body of a native method is given as a semicolon only, indicating that the implementation is omitted, in place of the method’s body or block.

See §8.4.3.4 of the jls for an example.

Strictfp methods: the effect of strictfp modifier in methods is to make sure that all float or double expressions within the method body are FP-strict.

Synchronized methods: a synchronized method acquires a monitor of an object or class before it executes. If the method is a class (static) method, the monitor associated with the Class object for the method’s class is used, else if an instance method, the monitor associated with the this object is used.

Generic methods: a method is generic if it declares one or more type variables, these being the formal type parameters of the method. The form of the formal type parameter list is identical to that of a class or interface.

The scope of a method’s type parameter is the entire declaration of the method, including the type parameter section. Hence, type parameters can appear as part of their own bounds, or as bounds of other type parameters declared in the same section.

Type parameters for methods are almost always inferred on invocation of the method, so they need not be explicitly provided.

Method return type: the return type of a method states whether the method doesn’t return a value, declared as void, or the type of value a method returns, if it returns a value.

Notion of return type substitutability: two methods are return type substitutable is the following conditions hold for both methods return types:
1. if one of the return types is a primitive type, then the second return type is identical to the first.

//return types for both methods identical
int myMethod(){}

int yourMethod(){}

2. if one of the return types is a reference type, then the other is substitutable to the first if it is a subtype of the earlier or can be converted to a subtype of the earlier by an unchecked conversion.

//where Lagos class was declared to extend Nigeria
//these two methods are return type substitutable
Nigeria myMethod(){}

Lagos yourMethod(){}

3. if the erasure of one of the return types is equal to the raw type of the other.
4. if one is void, then the other is void.

(to be continued)

1 comment:

Anonymous said...

у нас на официальном интернет-сайте подобран громадный набор [url=http://www.filmkpktut.ru/]клипы 320х240 для портативных mp4 плееров[/url].