use of access modifiers... how can I do this, this and this question

I

Indudhar

Hi Everyone,

Heres the logic.

objectOfClassA can access methods/variables of objectOfClassB
objectOfClassB can access methods/variables of objectOfClassC
And objectOfClassA cannot access methods/variables of objectOfClassC

How can I achieve the above? What various possibilities do I have?
Which one in your opinion is the best? Can I just use Inheritence to
implement the logic? Can I just use access modifiers to implement the
logic? How can I efficiently use both and other options if any, for
best results?

thanks,

Indu
 
R

Roedy Green

Heres the logic.

objectOfClassA can access methods/variables of objectOfClassB
objectOfClassB can access methods/variables of objectOfClassC
And objectOfClassA cannot access methods/variables of objectOfClassC

How can I achieve the above? What various possibilities do I have?
Which one in your opinion is the best? Can I just use Inheritence to
implement the logic? Can I just use access modifiers to implement the
logic? How can I efficiently use both and other options if any, for
best results?

Inheritance A -> B -> C won't work. C private B can't see. C package
A can see.

Putting A in a different package won't work. It will hide package
methods of C, but will also hide package methods of B. A can see
public methods of B and C though.

You create and interface for B that has only the methods you want A to
see in B. A politely refrains from direct access to B or C, only
going through interface references implemented only on B.

This does not prevent malicious use of methods, but provides some
control if you stick to the convention.
 
T

Thomas Weidenfeller

Indudhar said:
objectOfClassA can access methods/variables of objectOfClassB
objectOfClassB can access methods/variables of objectOfClassC
And objectOfClassA cannot access methods/variables of objectOfClassC

How can I achieve the above?

Is this homework?

Anyhow. Place B and C in one package, and A in another. Implement C as a
non-public class.
How can I efficiently use both and other options if any, for
best results?

Efficiently? Best results? In what respect? Number of lines of code?

/Thomas
 
I

Indudhar

Firstly thank you Roedy and Thomas for your replies. No this is not
homework, but this is work. I want to design something like this to
help us at my work. I want to design three groups of classes. groupA
<=> groupB <=> groupC groups talking to each other, but I want to
"programmatically" restrict access for groupA<=>groupC. We are
designing huge number of classes and 4 of us developers. We were just
sitting and discussing how we can do that and we had no idea. The only
way is, tell each of us just not do something like groupA<=>groupC even
though you can. Our web application uses Spring framework. Is there
something that we can use in Spring, to help us out with this?
Unfortunately we all are novices in Spring!! It was recommended that
we use it, and so we are in this together!!!
 
P

Patrick May

Indudhar said:
We are designing huge number of classes and 4 of us developers.

This is a red flag. Why are you designing a "huge number of
classes" up front? What do you mean by "huge"? Often this happens
when someone decides to wrap every table in an existing database in
its own class. There are almost always better solutions.

Regards,

Patrick
 

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,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top