Friday, April 13, 2007

CLASSES(2)

CLASSES(2)

Inner classes and enclosing instances: any class within another class is said to be nested, but a nested class that is not implicitly or explicitly declared static is said to be an inner class.
Examples illustrating rules on inner classes:

package examples;

class ByTheWay {
static int x = 4;
}

class MyOuter {

//nested class declared static
static class NotInnerClass{

//nested non-inner class can declare static members
//freely
static void trialMethod(){}

}

//nested class, not declared static, d4 inner class
//extending ByTheWay makes Inner inherit a static member, note
class InnerClass extends ByTheWay {

//if inner classes are to declare static members, they
//must be constants
static final String candeclare = "declared";

}

//nested member, interface, implicitly static
interface ImplicitStaticMember{

}

}

And here’s another example that deals with enclosing instances and static contexts.

package examples;

class AnotherExample {
String name="david";
//this inner class, MyInner, occurs in a static context
//due to static oldmethod. therefore it's lexically enclosing
//block that of oldmethod and it has no lexically enclosing
//class
static void oldmethod(){
final int m = 6;
class MyInner {
int y = m; //to use m in MyInner, declare it final
}
}

//inner class without a static context and the immediately
//lexically enclosing class of Without in AnotherExample
class Without {
String name="micheal";
}
}

class Loader {

public static void main(String[] args) {
//since the immediately enclosin instance of Without is
//AnotherExample, reflected in the instance creation
//constructor invocation for any object of type, Without.
//the name field exists for both classes
//creator attaches to its local field in scope
//i.e no naming conflict in same variable for inner and
//its enclosing variable
AnotherExample.Without creator = new AnotherExample().new
Without();
System.out.println("creator knows its name which is:
"+creator.name);

//AnotherExample is the 0th lexically enclosing instance
//of itself, i.e associated only with itself.
AnotherExample myself = new AnotherExample();
System.out.println("Associated with myself, i am:
"+myself.name);

}
}

Superclasses and Subclasses: the optional extends clause in a normal class declaration specifies the direct superclass of the current class.

Notation: super:
extends ClassType
a class is said to be a direct subclass of its direct superclass where the direct superclass is the class from whose implementation the implementation of the subclass is derived.

The direct superclass of an enum type E is Enum.

The extends clause must not appear in the definition of the class Object, because Object is a primordial class and has no direct superclass.

Given a class declaration which could possibly be or not be generic, i.e (C, n>0, c≠Object), the direct superclass of the class type is the type given in the extends clause when C is declared else Object becomes the direct superclass.

If C is a generic class declaration (C,n>0), given a parameterized class type C where for each T, i.e Ti, 1≤i≤n, T is a type, the direct superclass is D, where D is the direct superclass of C and theta is the substitution [F1 := T1, …, Fn := Tn].

Notes:
1. the class type must be an accessible, existing class type.
2. the class type must allow subclassing i.e should never be declared final
3. the class type must never name the class Enum or any of its invocations
4. you should always invoke parameterized types correctly, as declared and never invoke a wildcard type variable.

A subclass relationship is a transitive closure of the direct subclass relationship i.e if class A is a subclass of class C, then either of the following is true:
- A is direct subclass of C
- There exists a B such that A is a subclass of B and B is a subclass of C, applying this definition recursively.

A class C directly depends on a type T if T is mentioned as an extends or implements clause of C either as a superclass or superinterface, or as a qualifier of a superclass or superinterface name.

A class C depends on a reference type T if any of the following are true:
- C directly depends on T
- C directly depends on an interface I that depends on T
- C directly depends on a class D that depends on T.
Note: a class cannot depend on itself.

(to be continued)

No comments: