Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

Extending Inner Classes

Thread view: 
RFleming@NationalSteel.com - 08 Nov 2007 14:00 GMT
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
Larry Barowski - 08 Nov 2007 14:19 GMT
> Hello,
>
[quoted text clipped - 26 lines]
> 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.
Lew - 08 Nov 2007 14:33 GMT
> 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.

Signature

Lew

RFleming@NationalSteel.com - 08 Nov 2007 15:09 GMT
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!
Lew - 08 Nov 2007 15:32 GMT
Lew wrote:
>> There is no such thing as a "static inner class".  

> 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.

Signature

Lew

Lew - 08 Nov 2007 14:26 GMT
> I have a package Some Data.
>      A Class DataGroup1
[quoted text clipped - 12 lines]
>          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>

Signature

Lew

lyallex - 08 Nov 2007 14:28 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.