Results 1 to 4 of 4
  1. #1
    DeltaWarrior's Avatar
    DeltaWarrior is offline DeltaWarrior
    Join Date
    Dec 2011
    Location
    Bangalore
    Posts
    2
    Rep Power
    0

    Default Interface methods

    I'm a beginner too. I've been having the same problems. Could anyone just tell me what modifications must be made for the below program to work(that is no more"weaker access privileges" stuff)

    fandemo.java
    Java Code:
    interface fani
    {
    
     void switch_on();
     void switch_off();
     void faster();
     void slower();
     void state_info();
    
    }
    
    class fan implements fani
    {
     int power=0; /** POWER; 1=ON; 0=OFF*/
     int speed=1; /** SPEED=1,2,3,4. */
    
     void switch_on()
      {
       power=1; //switch on the fan
      }
    
     void switch_off()
     {
      power=0; //switch off the fan
     }
    
     void faster()
     {
      if(speed==4)return; //maximum speed
    
      else speed++; //increase speed one step
     }
    
     void slower()
     {
      if(speed==1)return; //minimum speed
    
      else speed--;
     }
    
     void state_info()
     {
      System.out.print("\n power="+power+" and speed="+speed);
      //print the state of fan
     }
    
    
    }
    
    
    
    
    class fandemo
    {
       public static void main(String args[])
       {
       int a;
       fan fan1=new fan();
       fan fan2=new fan(); //two fans
    
       System.out.println("Hello there!! \n");
       fan1.switch_on();
       fan1.faster();
       fan1.faster();
    
       fan1.state_info();
       System.out.printf("\n");
       fan2.state_info();
    
       }
    }
    The error is like the method in the class cannot implement the one in the interface etc.
    Pls help.

  2. #2
    rajivjoshi's Avatar
    rajivjoshi is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default Re: Interface problem

    wht if suppose we make the methods of the class fan as public with proper return type?
    Rajiv Joshi

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Interface methods

    I moved these posts from an old thread to this new thread. Next time don't resurrect an old zombie thread but start your own new thread.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    rajivjoshi's Avatar
    rajivjoshi is offline Member
    Join Date
    May 2010
    Posts
    13
    Rep Power
    0

    Default Re: Interface methods

    If you make the methods of the class fan as public it will work fine. i tired it please find the image.

    Interface methods-untitled.jpg
    Rajiv Joshi

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Replies: 1
    Last Post: 10-05-2011, 12:15 AM
  3. how to call or use methods inside interface?
    By akhmad in forum New To Java
    Replies: 2
    Last Post: 11-10-2010, 09:55 AM
  4. Methods that are NOT defined in interface
    By johto760 in forum New To Java
    Replies: 7
    Last Post: 10-29-2010, 06:31 PM
  5. Why methods in an interface cannot be static?
    By cbalu in forum Advanced Java
    Replies: 2
    Last Post: 12-12-2007, 07:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •