Results 1 to 5 of 5

Thread: Interfaces

  1. #1
    dalu is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Question Interfaces

    hi,
    I am greatly confused with the concept of Interfaces. Suppose my interface class is like:
    interface Bicycle {
    void changeCadence(int newValue);
    void changeGear(int newValue);
    void speedUp(int increment);
    void applyBrakes(int decrement);
    }

    and I write 2 implementation classes namely FirstCycle and SecondCycle implementing the class Bicycle . Now I want to use the changeGear method of class SecondCycle.
    If I call Bicycle.changeGear(int newValue), which method will be executed??? How to execute the changeGear method of the class SecondCycle??
    Or have I not got the correct concept???

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    If you implement an interface you are forced to implement each method in the interface in the implementing class. Let's make this a little easier to understand with an example
    Java Code:
    interface RideBike{
      void increaseSpeed(int speed);
    }
    
    class Bike implements RideBike{
      int speed;
      public void increaseSpeed(int speed){
        this.speed = speed;
      }
    }
    
    class MountainBike implements RideBike{
      int challenge; //1 is easy, 10 is hard
      int speed
      public void increaseSpeed(int speed){
        if(speed > this.speed){
          this.speed = speed;
          challenge++;
        }
        else{
          this.speed = speed;
          challenge--;
        }
      }
    }
    This is a very simple example. Both classes here override the method increaseSpeed, defined in the RideBike interface. You can do different things in the different versions of the class.

    You won't be calling Bicycle.changeGear, ever. This is not a static method so you can't call it statically, so you need an instance of some type to call the method on. You cannot instantiate an interface, so you create an object that extends that interface. In my example
    Java Code:
    Bike b = new Bike();
    b.increaseSpeed(10);
    You can also create the reference with the type of interface like this
    Java Code:
    RideBike rb = new Bike();
    rb.increaseSpeed(10);
    Also, consider viewing this:
    http://download.oracle.com/javase/tu...interface.html


    Your understanding is close, however; each class must have it's own distinct implementation of the methods in the interface(as shown above). You don't care how a bicycle does what it does, you just know it CAN do the things in the interface.
    Last edited by sunde887; 04-26-2011 at 10:03 AM.

  3. #3
    dalu is offline Member
    Join Date
    Mar 2011
    Posts
    23
    Rep Power
    0

    Default

    Thanks a lot...
    That really helped me. I worked out some problems and am getting the concept..

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I may have been a bit confusing, I hope I was fairly clear. The tutorials should get a lot more in-depth as you get into them further.

  5. #5
    sibernewf is offline Member
    Join Date
    Mar 2011
    Posts
    48
    Rep Power
    0

    Default

    Thanks Sunde.

    I also have problems with Interfaces. Your description is pretty good.
    My biggest problem is that I tend to put too many method signatures into the interface, when they dont all need to be there.

    Thanks for your explanation.

    Al

Similar Threads

  1. Interfaces
    By justin1980 in forum New To Java
    Replies: 9
    Last Post: 02-10-2010, 12:04 PM
  2. Interfaces
    By computerquip in forum New To Java
    Replies: 19
    Last Post: 09-08-2009, 04:58 PM
  3. Interfaces
    By jon80 in forum New To Java
    Replies: 2
    Last Post: 05-03-2008, 09:57 PM
  4. interfaces..
    By sireesha in forum New To Java
    Replies: 5
    Last Post: 01-16-2008, 05:52 PM
  5. Interfaces
    By imran_khan in forum New To Java
    Replies: 5
    Last Post: 07-30-2007, 08:11 AM

Posting Permissions

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