• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I extend an abstract inner class

 
Muni K Reddy
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Docherty
Bartender
Posts: 2953
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    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
    Posts: 11343
    Java Mac Safari
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 74
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Marc! That clears a few doubts!
    [ August 24, 2007: Message edited by: Muni K Reddy ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic