The Artima Developer Community
Sponsored Link

Java Answers Forum
Inheritance Inner class super

1 reply on 1 page. Most recent reply: Jan 14, 2003 9:38 AM by Antonas

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
sivanantham kandan

Posts: 3
Nickname: sivaparam
Registered: Dec, 2002

Inheritance Inner class super Posted: Jan 13, 2003 5:50 PM
Reply to this message Reply
Advertisement
Hi there,
I have appended two java code. ie. Chess.java and InheritInner.java

As I understand "super" is the key word to call
upper class constructor. It works fine with "Chess.java".
However, I am not able to understand and apply the same logic with "InheritInner.java".

Can someone explain logic of super in "InheritInner.java" ?
Why it is not calling outer class constructor "WithInner()"
but inner class constructor "Inner()".

Thanks
siva


====================================================

//: c06:Chess.java
// Inheritance, constructors and arguments.
class Game {
Game(int i) {
System.out.println("Game constructor");
}
}
class BoardGame extends Game {
BoardGame(int i) {
super(i);
System.out.println("BoardGame constructor");
}
}
public class Chess extends BoardGame {
Chess() {
super(11);
System.out.println("Chess constructor");
}
public static void main(String[] args) {
Chess x = new Chess();
}
} ///:~


====================================================

//: c08:InheritInner.java
// Inheriting an inner class.
class WithInner {
WithInner(){
System.out.println("WithInner");
}
class Inner {
Inner(){
System.out.println("Inner");
}
}
}
public class InheritInner extends WithInner.Inner {
//! InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
System.out.println("Siva");
InheritInner ii = new InheritInner(wi);
}
} ///:~


Antonas

Posts: 11
Nickname: tony
Registered: May, 2002

Re: Inheritance Inner class super Posted: Jan 14, 2003 9:38 AM
Reply to this message Reply
Taken from TIJ 2
"Because the inner class constructor must attach to a reference of the enclosing class object, things are slightly complicated when you inherit from an inner class. The problem is that the ?secret? reference to the enclosing class object must be initialized, and yet in the derived class there?s no longer a default object to attach to. The answer is to use a syntax provided to make the association explicit:


//: c08:InheritInner.java
// Inheriting an inner class.

class WithInner {
class Inner {}
}

public class InheritInner
extends WithInner.Inner {
//! InheritInner() {} // Won't compile
InheritInner(WithInner wi) {
wi.super();
}
public static void main(String[] args) {
WithInner wi = new WithInner();
InheritInner ii = new InheritInner(wi);
}
} ///:~
You can see that InheritInner is extending only the inner class, not the outer one. But when it comes time to create a constructor, the default one is no good and you can?t just pass a reference to an enclosing object. In addition, you must use the syntax

enclosingClassReference.super();

inside the constructor. This provides the necessary reference and the program will then compile.
"

Flat View: This topic has 1 reply on 1 page
Topic: Vectors Previous Topic   Next Topic Topic: ejb and context.close

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use