Tuesday, April 10, 2007

NAMES (2)

NAMES (2)
Shadowing declarations: when a declaration is in scope and visible at a point and another declaration using that name comes into being at the point, we say that the second same-named declaration shadows the first scoped and visible declaration at that point.

The example in §6.3.1 I believe is quite illustrative if you’ve been following this blog religiously. Questions?

Obscured declarations: where a simple name may potentially be referring to a variable, type or package, java will chose a variable in preference for a type and a type in preference to a package. Thus, in this situation, it may be impossible to refer to a visible type or package declaration via its simple name.
(Sorry, I tried obscuring in my favorite java compiler and it just keeps refusing my request. Maybe I was doing something wrong. Check yourself. )

MEMBERS AND INHERITANCE: Packages and reference types have members.

Members of type variables, parameterized types, raw types and intersection types: treated on earlier blogs.

Members of a package: the example below summarizes it.

package api_package;

//members of api_package are
//all top level class type of api_package
public class T {}

//all top level interface type of api_package
public interface IT{}

//all subpackages declared thus
package api_package.anotherpackage;

//Note: because a member of the package is T no other
//class type or interface type or subpackage which is
//member of this package can be so named!

The members of a class type: members of a class type are classes, interfaces, fields and methods. Members of a class are one of the following:
- members inherited from its direct superclass (if there is a superclass, otherwise from Object )
- members inherited from any direct superinterfaces
- members declared in the body of the class.
Note: constructors and type variables are not members of a class type.

package api_package;

public interface Colors {
int WHITE =0, BLACK = 1;}

public interface Separates {
int CYAN = 0, MAGENTA =1, YELLOW =2, BLACK =3;}

//TheClass inherits some fields from implemented interfaces
public class TheClass implements Colors, Separates{
//these are member fields declared
int x,y;
String mobi;

//this is a member method declared
void setClass(){}

//another member method declared with same name, setClass
//but different signature, note.the signature of a method
//is the simple name and the parameters of the method. setClass
//is said to be overloaded.
void setClass (int i){

}

//there is no restriction against a field, the String
//above and a method having same simple name, mobi!
void mobi (){}

//this is a member class declared
class InnerClass{}
}

public class Loader {
public static void main(String[] args){
//a class can only have two fields with the
//same simple names if they are declared in different
//interfaces and inherited, note!

//interface Colors, field BLACK
System.out.println("TheClass inherits a BLACK field from "
+ "Colors with value: "+Colors.BLACK);

//interface Separates, field BLACK
System.out.println("TheClass also inherits a BLACK field
from " +"Separates with value: "+Separates.BLACK);
}
}

The members of an interface type: the members of an interface type may be classes, interfaces, fields and methods. Members of an interface are:
- members declared in the interface
- those members inherited from direct superinterfaces
- every interface has an implicit undeclared method that is implemented by Object unless explicitly declared by the interface and if a method m is declared final in Object, no interface can explicitly declare it.
Note: type variables are not members.

Members of an array type: these section will be discussed in a later blog though, just illustrating with an example.

package api_package;

public class Loader {
public static void main(String[] args){
//aone array declared
int[] aone = new int[3];

//public final field, length, is a member of an array
System.out.println("aone array length is: "+aone.length);
for (int i=0; i<3;i++){
aone[i]=i;
System.out.println("Index "+i+" is "+aone[i]);
}

//the public method clone is a member that overrides
//same method in Object.
int[] atwo = aone.clone();
int pun = atwo.length;
System.out.println("atwo array length is: "+pun);
}
}

No comments: