Solved

can you give access to members of a class in a sub package to a class in it's parent package

Posted on 2004-09-20
9
168 Views
Last Modified: 2010-03-31
If I have a class in a package
package topPackage;

which instantiates a class from a sub package
package topPackage.subPackage;

is there a way to give the top package permission to the instanced classes members
without making them public, in other words can you grant permission into a package for another
package if it's a parent package?

The sub package has many, many data members
and it doesn't seem reasonable to make getter functions for each one, but maybe that is what I must do,
if I don't want to just make it public?
0
Comment
Question by:mitchguy
  • 4
  • 3
  • 2
9 Comments
 
LVL 2

Expert Comment

by:SergeiKo
ID: 12105252
Hello, mitchguy.

Assuming your situation as following.
package topPackage.subPackage;
class ClassWithManyMethods {
    /*    
     * many-many methods, may be all with protected or default visibility
     */
}

package topPackage;
class MainClass {
    // my work
}


I would think on adding some facade class into topPackage.subPackage package, e.g.

package topPackage.subPackage;

public class FacadeClass {
    /*    
     * restricted set of public methods
     */
}

So you can restrict by yourself the level of visibility of each part of data.

Regards.
0
 

Author Comment

by:mitchguy
ID: 12105622
Here is a modified test program which was used in another question I had, solved earlier for me
involving using packages.
The modifications I made were to try and print out the value of a string from each class, from
the class that instanstiated it. This test program models the structure of an application I have written
The actual application would have instead of one string in the bottom level package class, 10 to 60 data members of different types, depending on the class instanstiated
Here is a cut and paste of the solution test program  used in my previous question, with
modifications
-----------------------------------------------------------
Here are the files I'm compiling:
FILE: SubSub.java
------------------------

package topPackage.subPackage.subSubPackage;

public class SubSub
{
  public SubSub( )
  {
    subSubString = new String("Hello SubSub");
  }
 String subSubString;
}

FILE: Sub.java
----------------------

package topPackage.subPackage;
import topPackage.subPackage.subSubPackage.*;

public class Sub
{
  public Sub( )
  {
    subString = new String("Hello Sub");
    subSub = new SubSub( );
    System.out.println(subSub.subSubString);
    //this is where it won't compile unless I make subSubString public
  }
 String subString;
 SubSub subSub;
}

FILE: Top.java
-------------------------------

package topPackage;

import java.io.*;
import topPackage.subPackage.*;

public class Top
{
    public static void main(String[] args) throws IOException
    {
       Top top = new Top( );
    }

  public Top( )
  {
    topString = new String("HELLO TOP");
    System.out.println( topString) ;
    sub = new Sub( );
    System.out.println(sub.subString);//this won't compile unless I make subString public
  }
 String topString;
 Sub sub;
}

-------------------------------

They're in this structure:

[tyates@linux java]$ find topPackage/ -name *.java
topPackage/subPackage/subSubPackage/SubSub.java
topPackage/subPackage/Sub.java
topPackage/Top.java

-------------------------------

they compile like this:

[tyates@linux java]$ javac -classpath . topPackage/Top.java topPackage/subPackage/Sub.java topPackage/subPackage/subSubPackage/SubSub.java

--------------------------------

And run like this:

[tyates@linux java]$ java -cp . topPackage.Top



I want to know if their is a way to not have to make subString and subSubString public or make getter functions for this to compile
0
 
LVL 2

Expert Comment

by:SergeiKo
ID: 12105767
Hello.

I think links below will help you to decide what you want from packages and access specifiers.

http://codeguru.earthweb.com/java/tij/tij0057.shtml
http://codeguru.earthweb.com/java/tij/tij0058.shtml


Regards.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 37

Expert Comment

by:zzynx
ID: 12109649
>> it doesn't seem reasonable to make getter functions for each one, but maybe that is what I must do
Maybe it doesn't seem reasonable, but it's the way to go.
0
 

Author Comment

by:mitchguy
ID: 12111506
>>zzynx
So it sounds like the answer to the question of giving access to the parent package is no.
as for the gettor functions, I just don't want some one else to work with my code and
think it's crazy or bad code. so you would make getter functions if you were me?

This program has two different message formats for talking to different networked programs and
what I'm doing is converting data in one format to the other, each format has about fifty messages, with sometimes 50 fields so my code will look like

msgTypeA.x = msgTypeB.getx();

there will be hundreds,with potentially over a thousand lines like that, but if that's considered good safe programming???
 if so then, that's what I will do
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
ID: 12111561
>> so you would make getter functions if you were me?
Definitely

>>but if that's considered good safe programming
It is

I know it looks like some boring work to do.

Can't you make msgTypeA and msgTypeB part of the same package?
This way you could access their protected data members.

Reference: http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html
0
 

Author Comment

by:mitchguy
ID: 12111703
I could make them part of the same package but, each set of messages in their respective formats will have future independent programs using only one or the other, it's just this one program, which will use both so I was trying to keep them organized for ease of use down the road.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12111750
I see
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12111860
Thanks for accepting
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This video teaches viewers about errors in exception handling.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question