access Protected Fields from a class which does not have aconstructor

A

anu

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
 
L

Lew

anu said:
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

All classes have a constructor. Do you have the source for
'ServiceState'?
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:

What is the constructor declaration for 'ServiceState'?
public class ServiceStateProt extends ServiceState {
// public ServiceState(){

You have to name the constructor the same as the class:

public ServiceStateProt()
{
}
/*
You should move all the following logic out of the constructor. It
has no business in there, because its business is not construction.
*/
public void doWork()
{
        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();

Huh?

Don't create an instance of 'ServiceState' inside an instance of
'ServiceState' or its offspring.

All this statement does is obtain a foreign 'ServiceState' instance
then immediately throw it away.

What did you want it to do, specifically?
}
// closing braces on their own line no matter whose brace convention
you follow
Since, getting the intent, then getting the servicestate [sic] from the

Spelling, in particular case, counts.
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!

Notice that "Eclipse" (actually, not Eclipse - someone else is
delivering that error to Eclipse - you might investigate who) told you
what you are doing wrong. You declared a method 'ServiceState' in
your class, very badly named since it matches a type name, but did not
provide a return type. All methods must have return types, if only
'void'. Again, the error message was quite accurate.

Speaking of accurate error messages, it is odd that you would
paraphrase the error message instead of quoting it exactly (copy-and-
paste). It is as if you are trying to hamper the efforts of anyone
who wishes to help you.

<http://pscode.org/sscce.html>
 
I

Ian Shef

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!
 
S

sudeshsinghchandel

(e-mail address removed):




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).  This
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 first
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 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!- 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().
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top