Re: access Protected Fields from a class which does not have a constructor
anu <damnitall84@gmail.com> wrote in news:b47c9d39-414c-4f13-9e22-
bd7013c2baf3@j7g2000prm.googlegroups.com:
Hi,
I am pretty new to java and am working on writing some android
scripts. However, since the basic problem I have is about the java
programming, I thought I should post it here.
I have a class called ServiceState that is public class but 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
same class also has some fields that are protected. In order to access
these fields, I created a new class that inherits the ServiceState
class. The protected fields are visible in this class. 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:
public class ServiceStateProt 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 ServiceState class. But, all eclipse asks for is a return type for
the ServiceState method. What am I doing wrong? Please help!
Thanks a lot,
Anu
Java cannot create an instance of a subclass (such as ServiceStateProt)
without first creating an instance of the superclass (ServiceState). This
happens by either:
Java calls the default constructor of the superclass (if it exists and is
visible), OR
You must call an appropriate constructor of the superclass as the first
statement in the subclass's constructor.
You prevented the first from happening (perhaps by making the default
constructor of ServiceState private ?).
You have not done the second thing.
Notice that ServiceState() cannot be a constructor for ServiceStateProt.
Thus it must be a method, thus it needs a return type. A constructor for
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 constructor
in ServiceState and call it [ via super(...) ] as the first statement in
the constructor of ServiceStateProt.
Good Luck!