Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(regular) inner classes

 
Percy Dadabhoy
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can inner classes be subclassed only by inner classes in other classes?

Regards
Percy
 
Prasad Tamirisa
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be more eloborate on this..!
if you mean to ask
Can inner classes be subclassed only by inner classes with in outer class?

Then thats true.

An inner classes can extend other inner class defined with in the same outer class.

Ranchers,
correct me if i am wrong.
 
Meena R. Krishnan
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this out:
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also try this :-)
extending inner class outside Outer1's body:

 
Percy Dadabhoy
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ya John that's exactly what I mean. When I tried what you have given out there, I get the following-

Outer1.java:26: an enclosing instance that contains scjp.Outer1.Inner is required
class Outer2 extends Outer1.Inner { }
^
1 error

So I gather that if you want to override an inner class, you've got to do it with another Inner class.

Please let me know if there is anything anybody would like to add.

Regards
Percy
 
John Stone
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you are not able to compile my code?

Because, it works fine for me. It's based on one example from Thinking in Java 3rd edition, so it should also work with Java 1.4
 
Percy Dadabhoy
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by John Stone:
So, you are not able to compile my code?

Because, it works fine for me. It's based on one example from Thinking in Java 3rd edition, so it should also work with Java 1.4


I am using a Java 1.6 compiler. Could that be the problem? I then tried using the -source option of javac, I have appended below stuff that I tried on my PC-

C:\SCJPCode>javac -d classes -source 1.5 scjp\Outer1.java
scjp\Outer1.java:26: an enclosing instance that contains scjp.Outer1.Inner is required
class Outer2 extends Outer1.Inner { }
^
1 error

C:\SCJPCode>javac -d classes -source 1.4 scjp\Outer1.java
scjp\Outer1.java:26: an enclosing instance that contains scjp.Outer1.Inner is required
class Outer2 extends Outer1.Inner { }
^
1 error

So as you can see, no luck!!

Regards
Percy
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Percy, you have not added the constructor given by John.
Not
but
In your code a Standard-No-Args-Constructor will be inserted by the compiler and that won't work.
 
Percy Dadabhoy
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok...it works now...but I still don't understand why the constructor is making a difference.

Why doesn't it work like this? -
class Outer2 will have the standard-no-args constructor inserted by the compiler which will also have a no-args call super(). This call will invoke the standard-no-args constructor of the inner class viz. Outer1.Inner.

Or, is all this because of the fact that an instance of the inner class cannot exist without an instance of it's outer class.

The invocation o1.super() is new to me - don't we usually have only a call to super()?, is this method of invocation viz. o1.super() type, only for this special case of when extending an inner class?

Please explain.

Regards
Percy
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, is all this because of the fact that an instance of the inner class cannot exist without an instance of it's outer class.

The invocation o1.super() is new to me - don't we usually have only a call to super()?, is this method of invocation viz. o1.super() type, only for this special case of when extending an inner class?


I think that's exactly the point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic