Extending Inner Classes

Discussion in 'Java' started by RFleming@NationalSteel.com, Nov 8, 2007.

  1. Guest

    Hello,

    I have checked my Java In a nutshell, and Sun's documentation, and
    either there is no answer, or more likely I am asking it wrong when.

    I have a package Some Data.
    A Class DataGroup1
    A Method public ParseData getParseData
    ParseData retValue = new ParseData(args,args....);
    return retValue;

    A Public Static InnerClass ParseData
    A Method Public getParsedData

    A Class DataGroup2 extends DataGroup1
    A Method public ParseData getParseData
    ParseData retValue = new ParseData(args,args....);
    return retValue;

    A Public Static InnerClass ParseData
    A Method Public getParsedData



    The problem I am getting is I cannot seem to overide the superclasses
    method as DataGroup1.ParseData is a different return type than
    DataGroup2.ParseData. Am I doint something impossible as it is layed
    out above? I thought about making the inner class it's own separate
    class in the package, but if I do I would prefer it was 'protected' to
    be only viewable to classes in that package and do not know how to
    accomplish that. I could also just make the inner class part of the
    DataGroup Classes, but that would require some serious re-coding.

    Any suggestions, or hints would be greatly appreciated!

    Thanks

    Ryan
    , Nov 8, 2007
    #1
    1. Advertising

  2. Lew Guest

    wrote:
    > I have a package Some Data.
    > A Class DataGroup1
    > A Method public ParseData getParseData
    > ParseData retValue = new ParseData(args,args....);
    > return retValue;
    >
    > A Public Static InnerClass ParseData
    > A Method Public getParsedData
    >
    > A Class DataGroup2 extends DataGroup1
    > A Method public ParseData getParseData
    > ParseData retValue = new ParseData(args,args....);
    > return retValue;
    >
    > A Public Static InnerClass ParseData
    > A Method Public getParsedData


    I am not able to compile your example.
    <http://www.physci.org/codes/sscce.html>

    There is no such thing as a "static inner class". An inner class is not
    static, by definition. You mean a static nested class.

    > The problem I am getting is I cannot seem to overide the superclasses
    > method as DataGroup1.ParseData is a different return type than
    > DataGroup2.ParseData. Am I doint something impossible as it is layed
    > out above?


    I cannot say because you have not provided any code, but it looks wrong on the
    face of it. You say "ParseData()" is a method, but you spell it with an
    initial upper-case letter and indicate constructor syntax for it, which means
    it's a class and not a method. You have "ParseData" in every outer and nested
    class. You don't show the subclass is supposed to use ParseData, nor which
    one it's supposed to use. Your example is a mess.

    Review
    <http://www.physci.org/codes/sscce.html>
    and try again.

    > I thought about making the inner class it's own separate class in the package, but if I do I would prefer it was 'protected' to
    > be only viewable to classes in that package and do not know how to
    > accomplish that.


    You can make it a top-level class and declare it as package-private (no access
    modifier whatsoever). It can even be in the same source file as a public
    class, although that's probably not wise.

    > I could also just make the inner class part of the DataGroup Classes, but that would require some serious re-coding.


    Your outline suggest that you've already made the (not inner) nested class
    part of the DataGroup class(es?).

    > Any suggestions, or hints would be greatly appreciated!


    Provide an example we can actually run through our compilers.
    <http://www.physci.org/codes/sscce.html>

    More importantly, tell us what you're trying to accomplish. Not the specific
    strategy of overriding ParseData, whatever you think that might mean, but the
    goal of what such an ovverride is supposed to accomplish. If we don't know
    what you're trying to accomplish it's hard to help.

    <http://www.physci.org/codes/sscce.html>

    --
    Lew
    Lew, Nov 8, 2007
    #2
    1. Advertising

  3. <> wrote in message
    news:...
    > Hello,
    >
    > I have checked my Java In a nutshell, and Sun's documentation, and
    > either there is no answer, or more likely I am asking it wrong when.
    >
    > I have a package Some Data.
    > A Class DataGroup1
    > A Method public ParseData getParseData
    > ParseData retValue = new ParseData(args,args....);
    > return retValue;
    >
    > A Public Static InnerClass ParseData
    > A Method Public getParsedData
    >
    > A Class DataGroup2 extends DataGroup1
    > A Method public ParseData getParseData
    > ParseData retValue = new ParseData(args,args....);
    > return retValue;
    >
    > A Public Static InnerClass ParseData
    > A Method Public getParsedData
    >
    >
    >
    > The problem I am getting is I cannot seem to overide the superclasses
    > method as DataGroup1.ParseData is a different return type than
    > DataGroup2.ParseData. Am I doint something impossible as it is layed
    > out above? I thought about making the inner class it's own separate
    > class in the package, but if I do I would prefer it was 'protected' to
    > be only viewable to classes in that package and do not know how to
    > accomplish that. I could also just make the inner class part of the
    > DataGroup Classes, but that would require some serious re-coding.


    You have declared two unrelated classes DataGroup1.ParseData
    and DataGroup2.ParseData. In DataGroup2 you want a static inner
    class ParseData2 that extends ParseData. Alternately, getParseData()
    could return an interface type and DataGroup and DataGroup2
    could both implement that interface. Even though DataGroup2
    would not extend DataGroup in that case, you should still name it
    DataGroup2 or something else other than DataGroup to avoid
    confusion.
    Larry Barowski, Nov 8, 2007
    #3
  4. lyallex Guest

    wrote:
    > I thought about making the inner class it's own separate
    > class in the package, but if I do I would prefer it was 'protected' to
    > be only viewable to classes in that package and do not know how to
    > accomplish that.


    //Bar.java a public class in package com.foo

    package com.foo;

    public class Bar {

    }

    //Foo.java a class with 'default' visibility in package com.foo

    package com.foo;

    class Foo {

    }

    //Baz.java a client in package com.foo.baz

    package com.foo.baz;

    import com.foo.*;

    public class Baz {

    //OK, Bar is public
    Bar b = new Bar();

    //Won't compile, Foo only visible
    //to classes in package com.foo
    Foo f = new Foo();

    }

    Give the class default or package private visibility.
    You do this by omitting any visibility modifier (public, abstract,
    final). Of course I'm sure someone will come up with an answer to the
    rest of your quetion
    lyallex, Nov 8, 2007
    #4
  5. Lew Guest

    Larry Barowski wrote:
    > you want a static inner class ...


    Then you should prepare for disappointment:
    <http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.1.3>
    > An inner class is a nested class that is not explicitly or implicitly declared static.


    --
    Lew
    Lew, Nov 8, 2007
    #5
  6. Guest

    Thanks to all for your help.

    > There is no such thing as a "static inner class". An inner class is not static, > by definition. You mean a static nested class.


    My apologies, I am still somewhat of a new programmer to Java, I did
    not realize a static nested class existed. I usually do not use
    classes inside classes as I do not fully see a use for them, the
    program I inherited, already had this static nested class.

    > You can make it a top-level class and declare it as package-private (no access
    > modifier whatsoever).


    Thanks to Lew and lyallex (for your example), I have decided it's best
    for me to create a top level class and have my two classes DataGroup1
    and DataGroup2 access code from the ParseData class that way instead
    of using an inner static class.

    Again thanks for your help!
    , Nov 8, 2007
    #6
  7. Lew Guest

    Lew wrote:
    >> There is no such thing as a "static inner class".


    wrote:
    > My apologies,


    To whom are you apologizing? I don't think the JLS has feelings.

    The purpose of this newsgroup is to disseminate and discuss knowledge of Java
    programming. None of us knew any of this until we learned it. All of us have
    much to learn still.

    --
    Lew
    Lew, Nov 8, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. lonelyplanet999
    Replies:
    1
    Views:
    2,192
    VisionSet
    Nov 13, 2003
  2. Razvan
    Replies:
    5
    Views:
    11,272
    Dale King
    Jul 27, 2004
  3. Christian Bongiorno
    Replies:
    5
    Views:
    529
    Chris Uppal
    Aug 30, 2004
  4. Carlo v. Dango
    Replies:
    14
    Views:
    1,007
    Alex Martelli
    Oct 19, 2003
  5. Pyenos
    Replies:
    2
    Views:
    374
    Pyenos
    Dec 27, 2006
Loading...

Share This Page