Home | Blog | Java | Jokes | Poems | Musings | Site Map | Kudos | Downloads | Useful Sites | Interesting | System Setup | Contact

Home Page

AKGBackup - The backup program

            

 

Section 6 :  Overloading, Overriding, Runtime Type, and Object Orientation

 


 

Write code to construct instances of any concrete class including normal top level classes, inner classes, static inner classes, and anonymous inner classes.

 


 

Inner classes 

The mechanism of inner classes relates to scope and access, particularly access to variables in enclosing scopes. If a class is nested inside another, the two classes’ full names are OuterOne and OuterOne.InnerOne This format is similar to a class called InnerOne in a package called OuterOne. But it is illegal for a package and a class to have same name, so there can be no ambiguity. Although the format OuterOne.InnerOne works for the declaration of the type of an identifier, it does not reflect the real name of the class which is OuterOne$InnerOne on the disk and from the point of view of the Class class and class loaders. So if you try to load this class (OuterOne.InnerOne) using the Class.forName() method, the call will fail. The dollar separated name is also used if you print out the class name using methods getClass().getName() on an instance of the inner class. 

The enclosing this reference and construction of inner class 

When an instance of an inner class is created, there must be normally a preexisting instance of the outer class acting as context. This instance of outer class will be accessible from the inner object. The accessibility of the members of the outer class is crucial and very useful. It is possible because the inner class actually has a hidden reference to the outer class instance that was the current context when the inner class object was created. In effect this ensures that the inner class and the outer class belong together, rather than the inner class instance being just another member of the outer instance. 

Sometimes you might want to create an instance of  an inner class from a static method, or in some other situation where there is no ‘this’ object available. This situation arises in a main() method or if you need to create the inner class from a method of some object of an unrelated class. You can achieve this by using the new operator as though it were a member method of the outer class. Of course, you still must have an instance of the outer class. 

OuterOne.InnerOne i=new OuterOne.new InnerOne(); 

If you attempt to use the new operator to construct an inner object without a preexisting reference to an outer object, then the implied prefix ‘this’ is assumed. Since an instance of inner class must be created in context of an instance of the enclosing class, it follows that inner classes cannot have static members. Multiple objects of the inner classes can be associated with an object of an enclosing class at runtime. Though an implicit reference to the enclosing object is always available in every method and constructor of an inner class, it is possible to explicitly refer to the members in enclosing class, but this requires special usage of ‘this’ reference. 

InnerOne () {this.string = OuterOne.this.msg;} 

This use of ‘this’ construct is useful in cases where inner class members’ names are same as names of members of outer class. 

Inheritance in inner classes 

Inner classes can extend other classes and can themselves be extended. Therefore inheritance and containment hierarchy both must be considered when dealing with member access. In cases where the outer class and the class which inner class extends have members with same name, ambiguity arises because members of both can be accessed from the inner class. In such cases, the standard form of ‘this’ can be used to access members which are inherited. The keyword ‘super’ would be another alternative. To access the members from the enclosing context, the special form of ‘this’ together with the enclosing class name should be used. 

Static inner class or interface 

A static inner class does not have any reference to an enclosing object ( like, inside a static method there is no this reference ). Because of this methods of static inner class cannot access instance variables of the enclosing class. The net result is that a static inner class is really just a top level class with a modified naming scheme. In fact, you can use static inner classes as an extension to packaging. It can be instantiated without any reference to any instance of the enclosing class. 

Interfaces are implicitly static. Nested interfaces can optionally be prefixed with static and have public accessibility. There are no non static inner, local or anonymous interfaces.

The full name of a static nested class or interface includes the name of the enclosing class. A nested class cannot have the same name as an enclosing class or package. 

A static inner class can define both static and non static members, but its code can only directly access static member of enclosing class. Such classes can have any accessibility. 

Classes defined inside methods( local classes) 

A local class cannot be specified ‘static’. However, if the context is static, then the local class is implicitly static, otherwise, it is non static. Like an inner class, an instance of a non static local class is passed a hidden reference designating an instance of its enclosing class in its constructors, and this gives it much of the same capacity as non static inner classes. There are some restrictions:

  • Local classes cannot have static members.

  • They cannot have any access modifiers. This applies to local variables, and is also enforced for local classes.

Two particular aspects should be considered. First, an object created from an inner class within a method can have access to variables of the enclosing method. Second, it is possible to create an anonymous class. 

Any variable, either a local variable or a formal parameter ( of the method ) can be accessed by methods within an inner class, provided that variable is marked final. A final variable is effectively a constant, so this is perhaps a quite severe restriction, but the point is simply this : an object created inside a method is likely to outlive the method invocation. Since local variables and method arguments are conventionally destroyed when their method exits, these variables would be invalid for access by inner class methods after the enclosing method exits. By allowing access only to final variables, it becomes possible to copy the values of those variables into the object itself, thereby extending their lifetime. The other possible approaches to this problem would be to writing to two copies of the same data every time it got changed or putting local variables onto the heap instead of the stack. Either of these approaches would significantly degrade performance. 

Access rules for local classes

  • Can access members defined within the class.

  • Can access final local variables, final method parameters and final catch block parameters in the scope of the local context. Such final variables are also read only in the local class.

  • A non static local class can access members defined in enclosing class. However, a static local class can only access static member of enclosing class, but not non static members.

  • A non static local class can directly access members inherited by the enclosing class. However, a static local class can only directly access static members inherited by the enclosing class.

  • A local class can access members inherited from its super class in the usual way.

 Instantiating local classes 

Classes outside the context of a local class cannot access or create these classes directly, because after all they are local. A local class can be instantiated in the block in which it is defined. A method can return an instance of the local class. The local class type must them be assignable to the return type of the method. It cannot be the same as the local class type, since this type is not accessible outside of the method. Often a super type of the local class is specified as the return type. 

Creation of an object of non static local class requires an instance of the enclosing context. An instance of a static local class can be created by invoking either the class name or through an instance of the class, but no ‘this’ is passed in its constructor, as no outer object is involved.

As references to a local class cannot be declared outside of the local context, the functionality of the local class is only available through super type references.  

Anonymous classes (defined in a method) 

You cannot use new in the usual way to create an instance of an anonymous class since you do not know its name. Anonymous classes should be small. If the class has methods other than those of a simple, well known interface such as an AWT event listener, it probably should not be anonymous.  

Similarly, if the class has methods containing more than one or two lines of straightforward code, it probably should not be anonymous. The point here is that if you do not give the class a name, you have only the self documenting nature of the code itself to explain what it is for. 

  • An anonymous class cannot have a constructor.

  • An anonymous class can be a subclass of another explicit class, or it can implement a single explicit interface. It cannot do both.

  • If an anonymous class extends an existing class, rather than implementing an interface, then arguments for the super class constructor may be placed in the argument part of the new expression, like this :          new button (“Press Me”){// some code};

  • For anonymous classes implementing interfaces the parent class is Object, constructor for which takes no arguments, so it is impossible to use any arguments in the new part for these classes.

Since an anonymous class cannot define a constructor, an instance initializer can be used to achieve the same effect as a constructor.  No ‘extends’ clause is used in the construct.

 

As references to such a class cannot be declared, the functionality of the class is only available through super class references.

 

An anonymous class can provide a single interface implementation and no arguments are passed. The anonymous class implicitly extends Object class. No implements clause is used in the construct.

 

Like local classes, anonymous classes cannot have static members, and they cannot specify any accessibility modifiers.

 


 

section6-1 | section6-2 | section6-3

Sections : 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11

 

 

 

 

 

Home | Blog | Java | Jokes | Poems | Musings | Site Map | Kudos | Downloads | Useful Sites | Interesting | System Setup | Contact  

Loading

 Number of Pages viewed on this site since January' 2003 : Hit Counter eXTReMe Tracker

For any queries, comments or suggestions, write to me .

This site never compromises your privacy, please read this site's privacy policy.