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
162 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:mitchguy
Comment Utility
>>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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
I see
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thanks for accepting
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now