Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Selecting between base class and interface method...

Selecting between base class and interface method...

Scheduled Pinned Locked Moved Java
javahelp
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    faheemnadeem
    wrote on last edited by
    #1

    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.

    L 1 Reply Last reply
    0
    • F faheemnadeem

      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.

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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

      F 1 Reply Last reply
      0
      • L Luc Pattyn

        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

        F Offline
        F Offline
        faheemnadeem
        wrote on last edited by
        #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...

        F J 2 Replies Last reply
        0
        • F faheemnadeem

          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...

          F Offline
          F Offline
          faheemnadeem
          wrote on last edited by
          #4

          Getting an error while implementing the last method "getState"...

          L 1 Reply Last reply
          0
          • F faheemnadeem

            Getting an error while implementing the last method "getState"...

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            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

            F 1 Reply Last reply
            0
            • F faheemnadeem

              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...

              J Offline
              J Offline
              jschell
              wrote on last edited by
              #6

              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.

              F 1 Reply Last reply
              0
              • L Luc Pattyn

                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

                F Offline
                F Offline
                faheemnadeem
                wrote on last edited by
                #7

                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.

                L 1 Reply Last reply
                0
                • F faheemnadeem

                  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.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • J jschell

                    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.

                    F Offline
                    F Offline
                    faheemnadeem
                    wrote on last edited by
                    #9

                    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. :)

                    J 1 Reply Last reply
                    0
                    • F faheemnadeem

                      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. :)

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #10

                      public class Worldlamp extends Service
                      {
                      ...
                      }

                      public class LampProxy implements ILamp
                      {
                      LampProxy(Worldlamp w)
                      {
                      }
                      }

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups