aspose file tools*
The moose likes Beginning Java and the fly likes How do I extend an abstract inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I extend an abstract inner class" Watch "How do I extend an abstract inner class" New topic
Author

How do I extend an abstract inner class

Muni K Reddy
Ranch Hand

Joined: Aug 23, 2007
Posts: 74
Hello Guys,
If I have an abstact/non-abstract class and an abstract inner class, How can I extend this inner class?

Does it make any sense in real world to make an inner class abstract??

Thanks!

[ August 23, 2007: Message edited by: Muni K Reddy ]
[ August 23, 2007: Message edited by: Muni K Reddy ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I'm not sure what "an abstact/non-abstract class" is, but have you tried using an abstract nested class? It works very much like a top-level abstract class.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2312
    
  49
Does it make any sense in real world to make an inner class abstract??
Absolutely. If your class requires the use of a number of other classes that are related to each other (ie form a class hierarchy) and those classes are particular to the parent class (ie are ideal candidates for implementation as inner classes) then it is probable that the super class of the inner class hierarchy will be abstract.
Muni K Reddy
Ranch Hand

Joined: Aug 23, 2007
Posts: 74
Originally posted by marc weber:
I'm not sure what "an abstact/non-abstract class" is, but have you tried using an abstract nested class? It works very much like a top-level abstract class.


Mark, I meant abstract or a non abstract top level class but specifically having a nested abstract inner class .
  • How do I extend this abstract inner class?
  • Is it of any value if I make the nested inner class abstract?
  • Muni K Reddy
    Ranch Hand

    Joined: Aug 23, 2007
    Posts: 74
    Absolutely. If your class requires the use of a number of other classes that are related to each other (ie form a class hierarchy) and those classes are particular to the parent class (ie are ideal candidates for implementation as inner classes) then it is probable that the super class of the inner class hierarchy will be abstract.


    Tony, I meant marking the nested inner class abstract, not the outer class! Does it make sense?
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Muni K Reddy:
    ...
  • How do I extend this abstract inner class?
  • Is it of any value if I make the nested inner class abstract?
  • Tony's explanation applies to abstract classes in general, whether they are top-level or nested. It's the same idea, just in the context of an enclosing class.

    As for how to extend an abstract nested class... It's the same as extending a concrete nested class, except that you need to implement any abstract methods (just as you would if you were extending a top-level abstract class).

    The best way to learn about this is to try writing some code.
    Muni K Reddy
    Ranch Hand

    Joined: Aug 23, 2007
    Posts: 74

    Tony's explanation applies to abstract classes in general, whether they are top-level or nested. It's the same idea, just in the context of an enclosing class.

    As for how to extend an abstract nested class... It's the same as extending a concrete nested class, except that you need to implement any abstract methods (just as you would if you were extending a top-level abstract class).

    The best way to learn about this is to try writing some code.


    Mark, After a bit of fighting I did this.


    When I had posted the question I tried to do something like this:

    Does this imply that the outer class needs to be extended before any of the inner classes can be extended ?. I guess it does not make sense to directly inherit an inner class.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    Originally posted by Muni K Reddy:
    ... Does this imply that the outer class needs to be extended before any of the inner classes can be extended ? ...

    Not necessarily, because you can extend a nested class within the same enclosing class. For example...

    Also note that in your code, A1 extends Test, so A1 inherits the nested class TestINNER. Therefore, you can just say "A2 extends TestINNER" instead of "A2 extends Test.TestINNER."

    Here are some other things you can do...
    Muni K Reddy
    Ranch Hand

    Joined: Aug 23, 2007
    Posts: 74
    Thanks Marc! That clears a few doubts!
    [ August 24, 2007: Message edited by: Muni K Reddy ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How do I extend an abstract inner class