• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

(regular) inner classes

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can inner classes be subclassed only by inner classes in other classes?

Regards
Percy
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this out:
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic