Re: access Protected Fields from a class which does not have a
constructor
On Aug 13, 1:19 am, Ian Shef <inva...@avoiding.spam> wrote:
anu <damnital...@gmail.com> wrote in news:b47c9d39-414c-4f13-9e22-
bd7013c2b...@j7g2000prm.googlegroups.com:
Hi,
I am pretty new tojavaand am working on writing some android
scripts. However, since the basic problem I have is about thejava
programming, I thought I should post it here.
I have aclasscalled ServiceState that is publicclassbut does not
have a constructor (when I try to use the default constructor in
Eclipse, the error is given that the constructor is not visible). This
sameclassalso has some fields that are protected. In order to access
these fields, I created a newclassthat inherits the ServiceState
class. The protected fields are visible in thisclass. However, I keep
getting the message that the default constructor is not visible for
the superclass (i.e. ServiceState) and that a constructor needs to be
invoked explicitly. But, I am not quite sure how to do this. I tried
the following:
publicclassServiceStateProt extends ServiceState {
public ServiceState(){
Intent in = new Intent("SERVICE_STATE_CHANGED_ACTION");
Handler target = new Handler();
Context ctx = null;
PhoneStateIntentReceiver psir = new
PhoneStateIntentReceiver(ctx,target);
psir.onReceiveIntent(ctx, in);
psir.registerIntent();
psir.notifyServiceState(22);
ServiceState ss = psir.getServiceState();
}
}
Since, getting the intent, then getting the servicestate from the
object of PhoneStateIntentReceiver is how you can get an instance of
the ServiceStateclass. But, all eclipse asks for is a return type for
the ServiceState method. What am I doing wrong? Please help!
Thanks a lot,
Anu
Javacannotcreate an instance of a subclass (such as ServiceStateProt)
without first creating an instance of the superclass (ServiceState). T=
his
happens by either:
Javacalls the default constructor of the superclass (if it exists and =
is
visible), OR
You must call an appropriate constructor of the superclass as the fir=
st
statement in the subclass's constructor.
You prevented the first from happening (perhaps by making the default
constructor of ServiceStateprivate?).
You have not done the second thing.
Notice that ServiceState()cannotbe a constructor for ServiceStateProt. =
Thus it must be a method, thus it needs a return type. A constructor f=
or
ServiceStateProt would be named ServiceStateProt.
Solutions:
It is hard to suggest a solution becuase you did not provide the code for
ServiceState. However, either make the default consttructor of
ServiceState accessible (e.g. public), or create an accessible constructo=
r
in ServiceState and call it [ via super(...) ] as the first statement in
the constructor of ServiceStateProt.
Good Luck!- Hide quoted text -
- Show quoted text -
Constructor in Java is not availabe for overriding.
So in this case , You can move constructor ServiceState() to class
ServiceState
and call this constructor from the constructor of dervied class
ServiceStateProt using super().