Another Inheritance Puzzle ...
/* This program will test whether we can inherit a nested class
* by an outer class or no.
*/
class OuterClass{
OuterClass(){
System.out.println("OuterClass Constructor");
}
// Nested class
publicclass NestedClass{
NestedClass(){
System.out.println("NestedClass Constructor");
}
void display(){
System.out.println("Display of NestedClass");
}
}
}
// Class ToDeriveClass inherits the NestedClass defined in OuterClass
class ToDeriveClassextends OuterClass.NestedClass{
ToDeriveClass(){
// Calls constructor of NestedClass
super();//Generates Error!
System.out.println("ToDeriveClass Constructor");
}
}
class InheritanceEx3{
publicstaticvoid main(String[] args){
ToDeriveClass tdc =new ToDeriveClass();
OuterClass oc =new OuterClass();
// Calls display of NestedClass
oc.new NestedClass().display();
}
}
/* ERROR GENERATED */
/* InheritanceEx3.java:31: an enclosing instance that contains OuterClass.NestedClass is required
super();
^
1 error
*/
[2440 byte] By [
nix365a] at [2007-11-27 6:43:30]

There is a klugy way to allow the nested class top-level visibility, and that is to make it static:
class OuterClass
{
OuterClass()
{
System.out.println("OuterClass Constructor");
}
// Nested class
public static class NestedClass
{
NestedClass()
{
System.out.println("NestedClass Constructor");
}
void display()
{
System.out.println("Display of NestedClass");
}
}
}
Derive does not need changing w/ this and super will be called just fine.
Then change InheritanceEx3 to something like:
class InheritanceEx3
{
public static void main(String[] args)
{
ToDeriveClass tdc = new ToDeriveClass();
OuterClass oc = new OuterClass();
new OuterClass.NestedClass().display();
}
}
Message was edited by:
petes1234
More information on static nested classes can be found [url=http://java.sun.com/docs/books/tutorial/java/javaOO/nested.html]here[/url]. The Sun tutorial states:
Static Nested Classes
As with class methods and variables, a static nested class is associated with its outer class. And like static class methods, a static nested class cannot refer directly to instance variables or methods defined in its enclosing class ?it can use them only through an object reference.
--
Note: A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.
--
Static nested classes are accessed using the enclosing class name:
OuterClass.StaticNestedClass
For example, to create an object for the static nested class, use this syntax:
OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
> i also think if there is no real aim beside doing
> such experiments, it's a bit pointless since you'll
> prolly never have to face such design patterns
I disagree strongly. I think most of us learn coding best by hands-on manipulation, by playing with it. Even if the code seems pointless at the time, our knowledge about how and why it works increases, and our comfort level with the information improves. It's really a win-win situation.