aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes (regular) inner classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "(regular) inner classes" Watch "(regular) inner classes" New topic
Author

(regular) inner classes

Percy Dadabhoy
Greenhorn

Joined: Mar 26, 2007
Posts: 26
Can inner classes be subclassed only by inner classes in other classes?

Regards
Percy
Prasad Tamirisa
Ranch Hand

Joined: Mar 26, 2007
Posts: 130
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.


Regards,
Durga Prasad
Meena R. Krishnan
Ranch Hand

Joined: Aug 13, 2006
Posts: 178
Try this out:
John Stone
Ranch Hand

Joined: May 04, 2007
Posts: 332
also try this :-)
extending inner class outside Outer1's body:

Percy Dadabhoy
Greenhorn

Joined: Mar 26, 2007
Posts: 26
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

Joined: May 04, 2007
Posts: 332
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

Joined: Mar 26, 2007
Posts: 26
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

Joined: Mar 29, 2007
Posts: 115
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

Joined: Mar 26, 2007
Posts: 26
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

Joined: Mar 29, 2007
Posts: 115
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: (regular) inner classes