Newsgroup: it.comp.java
Titolo: Private

Hi,
How do I gain access to a variable that belongs to another class, and has
been declared private? I am not permitted to modify the other class.
Thanks.
--
Regards,
Bastion.

 been declared private? 
You don't. That's the concept of having "private" variables.
BTW, the "it" in the beginning of the name of this newsgroup means
"italian". This is an italian-speaking news group.
Davide

 Bastion.
-- 
questo articolo e` stato inviato via web dal servizio gratuito 
http://www.newsland.it/news
 segnala gli abusi ad 
ab...@newsland.it

Oh, okay.
I thought it was IT (Information Technology)
Thanks.
--
Regards,
Bastion.

Thanks.
Well, we have a little task to do which involves inheritance. TaxiTrip is
the superclass and LimoTrip is the subclass.
The super class (taxi) has three private variables, that is: hireFee, rate
and distanceTravelled. We are NOT allowed to modify this class.
The class also contains
1. getFare() which returns the cost, which is
hireFee+distanceTravelled*rate.
2. toString()
3. setRate()
4. getRate() which returns the rate
The limo trip has the same hireFee, but the rate is twice that of the taxi.
It also has a 75｢/passenger fee. The only trouble I have is getting the
hireFee distanceTravelled.
How do you suggest I get the two variables WITHOUT modifying the taxi class?
Thanks.
--
Regards,
Bastion.

 Bastion.


I get an error message:
For the code: return (int)(distanceTravelled);
LimoTrip.java:12: distanceTravelled has private access in TaxiTrip
--
Regards,
Bastion.

 Bastion.


Ok. Thanks.
Well, here's the code that I used for the LIMO class.
It works.
public double getFare ( ) {
  // calcDist to work out distance travelled since it is private in super
  double calcDist = ((super.getFare()-4.5)/getRate());
  return (int)((2*getRate()*calcDist+numberPassengers*
passengerCharge)+(4.5))*100)/100.0;
}
--
Regards,
Bastion.

 class? 
There are two issues here. The first is theoretical: if you have a class 
which hides its implementation, or has not been built for inheritance, and 
you cannot change the source, you cannot use the hidden parts (actually 
there's a trick for that, involving reflection and accessiblity, but you 
dont want to go there :). As simple as that. It's not as much that the 
problem is unsolvable, is that isnt correct to try and solve it: if the 
members are private, they were meant to be private (or perhaps not: but how 
do you know?) - you cannot even know when and how they are updated with 
respect to the exposed interface. 
A typical example's the class-loading mechanism in Java 2, which is fairly 
complex, and whose specification is publicly available... but the 
implementation in Sun's JVM is not. It's there and works fine (classes get 
loaded alright all the time :) but cannot be accessed outside the JVM code 
itself.
The second is practical: how to *do* the job anyway? What you can do is 
either to replicate the behaviour of the class (in your case, would mean 
rewriting the original one) and using the original one to develop tests 
which compare results - so that you make sure that you've written the right 
code; or, to use properties of the result - and knowledege about the 
services of the class - to backtrack to the information that you want. 
That's typical of your case, when what you want is basically to access an 
intermediate result used in a calculation exposed by your class, and the 
calculation is invertible. In this case, you will inherit from your class 
and add a method that invokes the original methods and then does the 
"reverse" bit of calculation that computes the middle result.
Of course it's less performant, but having a "closed" implementation, you 
*dont* really have the intermediate result - so that's you've to live with.
-- 
Life's something u don't get out alive..
ObjectZone - 
http://space.tin.it/computer/csadun

 wrote in message
news:Xns92A2871A3F371cr...@130.133.1.4...


 But thank you anyway :-)
The short version is: you can't access it, recalculate it. :-)


 wrote in message
news:Xns92A28ECA7AA18cr...@130.133.1.4...


 Davide
Non ci vuole molto a capirlo comunque
basta leggere qualche post, sono tutti
in italiano.
Non ho mai postato in un newsgroup senza
aver letto prima qualche post, se fosse
stato in inglese me ne sarei accorto.
Comunque hai fatto bene a rispondergli lo
stesso (ho visto altri che si sono limitati
a digli il fatto dell'it).
                danilo