Selecting between base class and interface method...
-
Hi, I have a class "worldstore" that is derived from "Service" class and implements interface "ILamp". I have a method called "getState" in ILamp interface which returns a "boolean" and another method is available with the same name in "Service" base class which returns an "int". While implementing the methods of the interface in "worldstore" class, I get an error when implementing "getState" from ILamp. Whereby, eclipse detects it to belonging to the base "Service" class rather than that of the ILamp interface, and therefore asks me to change the return type to boolean. All i want is to implement the "getState" method from the interface. Any solutions please... I am using java 1.6 Thanks.
-
Hi, I have a class "worldstore" that is derived from "Service" class and implements interface "ILamp". I have a method called "getState" in ILamp interface which returns a "boolean" and another method is available with the same name in "Service" base class which returns an "int". While implementing the methods of the interface in "worldstore" class, I get an error when implementing "getState" from ILamp. Whereby, eclipse detects it to belonging to the base "Service" class rather than that of the ILamp interface, and therefore asks me to change the return type to boolean. All i want is to implement the "getState" method from the interface. Any solutions please... I am using java 1.6 Thanks.
I think your question got answered here[^]. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
I think your question got answered here[^]. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3Thanks for the reference link... But still i am unable to grasp the solution being a novice programmer. Should i make another interface that links to my "ILamp" interface. And then implement the new one in my worldlamp class. Ok for a little help here is my current current class structure:
package mw;
import org.mundo.bas.DoILamp;
import org.mundo.bas.ILamp;
import org.mundo.rt.Service;
import org.mundo.rt.Session;
import org.mundo.rt.Signal;public class worldlamp extends Service implements ILamp {
// Private member variables private String CHANNEL\_NAME = ""; private static final String ZONE\_NAME = "lan"; private DoILamp doLamp; /\* \* Default Constructor \*/ public worldlamp(String ch) { CHANNEL\_NAME = ch; } /\* \* Fired on service initialized \* @see org.mundo.rt.Service#init() \*/ public void init() { try { // Subscribe to channel Signal.connect( getSession().subscribe(ZONE\_NAME, CHANNEL\_NAME), this); // Initialize stub doLamp = new DoILamp(); // connect stub for publishing Signal.connect(doLamp, getSession().publish(ZONE\_NAME, CHANNEL\_NAME)); } catch (Exception x) { x.printStackTrace(); } } @Override public void setState(boolean b) { doLamp.setState(b); } @Override public boolean getState() { return doLamp.getState(); }
}
And the ILamp Interface
package org.mundo.bas;
import org.mundo.rt.ActiveArray;
import org.mundo.rt.ActiveMap;//@mcRemote(className="org.mundo.bas.ILamp")
public interface ILamp
{
public void setState(boolean b);
public boolean getState();
}I don't have the source for "Service". Its a Jar library. And i can't change the ILamp interface too... It is generated from a script... so has to stay the same... Kindly can you please give me a solution to this...
-
Thanks for the reference link... But still i am unable to grasp the solution being a novice programmer. Should i make another interface that links to my "ILamp" interface. And then implement the new one in my worldlamp class. Ok for a little help here is my current current class structure:
package mw;
import org.mundo.bas.DoILamp;
import org.mundo.bas.ILamp;
import org.mundo.rt.Service;
import org.mundo.rt.Session;
import org.mundo.rt.Signal;public class worldlamp extends Service implements ILamp {
// Private member variables private String CHANNEL\_NAME = ""; private static final String ZONE\_NAME = "lan"; private DoILamp doLamp; /\* \* Default Constructor \*/ public worldlamp(String ch) { CHANNEL\_NAME = ch; } /\* \* Fired on service initialized \* @see org.mundo.rt.Service#init() \*/ public void init() { try { // Subscribe to channel Signal.connect( getSession().subscribe(ZONE\_NAME, CHANNEL\_NAME), this); // Initialize stub doLamp = new DoILamp(); // connect stub for publishing Signal.connect(doLamp, getSession().publish(ZONE\_NAME, CHANNEL\_NAME)); } catch (Exception x) { x.printStackTrace(); } } @Override public void setState(boolean b) { doLamp.setState(b); } @Override public boolean getState() { return doLamp.getState(); }
}
And the ILamp Interface
package org.mundo.bas;
import org.mundo.rt.ActiveArray;
import org.mundo.rt.ActiveMap;//@mcRemote(className="org.mundo.bas.ILamp")
public interface ILamp
{
public void setState(boolean b);
public boolean getState();
}I don't have the source for "Service". Its a Jar library. And i can't change the ILamp interface too... It is generated from a script... so has to stay the same... Kindly can you please give me a solution to this...
Getting an error while implementing the last method "getState"...
-
Getting an error while implementing the last method "getState"...
you should read the error message carefully, it is meant to tell you what is wrong. And if you can't figure it out, don't tell us there is "an error", give the detailed error message. The clue is in the first answer at SO, basically in
void IControl.Paint() { System.Console.WriteLine("IControl.Paint"); } void ISurface.Paint() { System.Console.WriteLine("ISurface.Paint"); }
you see the interface name is prefixed to the method name, that is how they get discriminatated when defining them, and if necessary also when calling one of them (it would be necessary if the "signature", i.e. the list of argument types, is the same and hence not telling the methods apart; the method matching is based on method name and parameter list, and NOT on the return type. If you are convinced you need all this, and it is beyond your current knowledge of the language, my best advice for you is to choose, buy and study an introductory book on Java. That will teach you all the basics, in a consistent and speedy way. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
Thanks for the reference link... But still i am unable to grasp the solution being a novice programmer. Should i make another interface that links to my "ILamp" interface. And then implement the new one in my worldlamp class. Ok for a little help here is my current current class structure:
package mw;
import org.mundo.bas.DoILamp;
import org.mundo.bas.ILamp;
import org.mundo.rt.Service;
import org.mundo.rt.Session;
import org.mundo.rt.Signal;public class worldlamp extends Service implements ILamp {
// Private member variables private String CHANNEL\_NAME = ""; private static final String ZONE\_NAME = "lan"; private DoILamp doLamp; /\* \* Default Constructor \*/ public worldlamp(String ch) { CHANNEL\_NAME = ch; } /\* \* Fired on service initialized \* @see org.mundo.rt.Service#init() \*/ public void init() { try { // Subscribe to channel Signal.connect( getSession().subscribe(ZONE\_NAME, CHANNEL\_NAME), this); // Initialize stub doLamp = new DoILamp(); // connect stub for publishing Signal.connect(doLamp, getSession().publish(ZONE\_NAME, CHANNEL\_NAME)); } catch (Exception x) { x.printStackTrace(); } } @Override public void setState(boolean b) { doLamp.setState(b); } @Override public boolean getState() { return doLamp.getState(); }
}
And the ILamp Interface
package org.mundo.bas;
import org.mundo.rt.ActiveArray;
import org.mundo.rt.ActiveMap;//@mcRemote(className="org.mundo.bas.ILamp")
public interface ILamp
{
public void setState(boolean b);
public boolean getState();
}I don't have the source for "Service". Its a Jar library. And i can't change the ILamp interface too... It is generated from a script... so has to stay the same... Kindly can you please give me a solution to this...
faheemnadeem wrote:
Kindly can you please give me a solution to this..
Almost always the answer to this sort of problem is that someone is overusing inheritance. But if you must implement both then you MUST have two classes. To achieve functional equivalence you can create a proxy for one case. So your ILamp proxy class would take an instance of the worldlamp class in its constructor.
-
you should read the error message carefully, it is meant to tell you what is wrong. And if you can't figure it out, don't tell us there is "an error", give the detailed error message. The clue is in the first answer at SO, basically in
void IControl.Paint() { System.Console.WriteLine("IControl.Paint"); } void ISurface.Paint() { System.Console.WriteLine("ISurface.Paint"); }
you see the interface name is prefixed to the method name, that is how they get discriminatated when defining them, and if necessary also when calling one of them (it would be necessary if the "signature", i.e. the list of argument types, is the same and hence not telling the methods apart; the method matching is based on method name and parameter list, and NOT on the return type. If you are convinced you need all this, and it is beyond your current knowledge of the language, my best advice for you is to choose, buy and study an introductory book on Java. That will teach you all the basics, in a consistent and speedy way. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3Ok the i am getting an error on...
public boolean getState() { return doLamp.getState(); }
And the error is... "The return type is incompatible with Service.getState()". Now if i change the implementation to
public boolean ILamp.getState() { return doLamp.getState(); }
No i am not allowed to do this again gives an error "Return type for the method is missing". if i read rightly in forum message u provided that the explicit implementations are allowed in C# and not in JAVA. P.s. i am reading a book on java but this is new for me and i can't understand so some help would be really grateful.
-
Ok the i am getting an error on...
public boolean getState() { return doLamp.getState(); }
And the error is... "The return type is incompatible with Service.getState()". Now if i change the implementation to
public boolean ILamp.getState() { return doLamp.getState(); }
No i am not allowed to do this again gives an error "Return type for the method is missing". if i read rightly in forum message u provided that the explicit implementations are allowed in C# and not in JAVA. P.s. i am reading a book on java but this is new for me and i can't understand so some help would be really grateful.
Sorry. I see, I only skimmed the reference I gave you; after a careful read it tells me it can't be done in Java, at least not in the way it gets done in C#, which I am familiar with. So you'd have to go another route, such as the proxy jschell suggested. :-O
Luc Pattyn [My Articles] Nil Volentibus Arduum
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Please use <PRE> tags for code snippets, they improve readability.
CP Vanity has been updated to V2.3 -
faheemnadeem wrote:
Kindly can you please give me a solution to this..
Almost always the answer to this sort of problem is that someone is overusing inheritance. But if you must implement both then you MUST have two classes. To achieve functional equivalence you can create a proxy for one case. So your ILamp proxy class would take an instance of the worldlamp class in its constructor.
Can you please give me a small sample implementation based on the structure given above... as i have no clue about proxy classes. I would be really thankful. :)
-
Can you please give me a small sample implementation based on the structure given above... as i have no clue about proxy classes. I would be really thankful. :)