Extending Inner Classes

R

RFleming

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
 
L

Lew

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

Larry Barowski

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

lyallex

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
 
R

RFleming

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!
 
L

Lew

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top