newbie interface question

Discussion in 'Java' started by happytoday, Aug 28, 2012.

  1. happytoday

    happytoday Guest

    Dear All,
    I am testing interface but really I can not apply what is explained in
    oracle tutorials . Could you please explain why I have errors with
    this code :

    //*data of class Bicycle is cadence , gear , speed
    //*methods of class Bicycle is changeCadence ,
    IncreaseGearToIncreaseSpeed , IncreadSpeed , DeareaseSpeed
    //*data of class Bicycle is Cadence , Gear , Speed

    class prog01_BicycleDemo_inheirtance_trial01
    {
    public static void main (String [] argc )
    {
    Bicycle bike1 = new Bicycle();
    MountainBicycle bike2 = new MountainBicycle();

    bike1.speed=1;
    bike1.cadence=1;
    bike1.gear=1;
    bike1.printresults();


    bike1.speed=1;
    bike1.cadence=1;
    bike1.gear=1;
    bike1.printresults();

    bike1.speed=1;
    bike1.cadence=1;
    bike1.gear=1;
    bike1.printresults();

    bike2.speed=1;
    bike2.cadence=1;
    bike2.gear=1;
    bike2.printresults();

    bike2.mb_speed=1;
    bike2.mb_cadence=1;
    bike2.mb_gear=1;
    bike2.mb_printresults();
    }
    }

    class Bicycle
    {
    int speed;
    int cadence;
    int gear ;

    void speed(int newspeed)
    {
    speed=newspeed;
    }

    void cadence(int newcadence)
    {
    cadence=newcadence;
    }

    void gear(int newgear)
    {
    gear=newgear;
    }

    void printresults()
    {
    System.out.println( "Cadence : " + cadence + " Gear : " + gear + "
    Speed :" + speed );
    }
    }

    class MountainBicycle extends Bicycle
    {
    int mb_speed;
    int mb_cadence;
    int mb_gear ;

    void mb_speed(int newmb_speed)
    {
    mb_speed=newmb_speed;
    }

    void mb_cadence(int newmb_cadence)
    {
    mb_cadence=newmb_cadence;
    }

    void mb_gear(int newmb_gear)
    {
    mb_gear=newmb_gear;
    }

    void mb_printresults()
    {
    System.out.println( "MB_Cadence : " + mb_cadence + " MB_Gear : " +
    mb_gear + " MB_Speed :" + mb_speed );
    }
    }
     
    happytoday, Aug 28, 2012
    #1
    1. Advertising

  2. happytoday

    Eric Sosman Guest

    On 8/27/2012 7:17 PM, happytoday wrote:
    > Dear All,
    > I am testing interface but really I can not apply what is explained in
    > oracle tutorials . Could you please explain why I have errors with
    > this code :
    > [...]


    What "errors" do you have? For me, the code compiles just
    fine, and runs producing the output

    Cadence : 1 Gear : 1Speed:1
    Cadence : 1 Gear : 1Speed:1
    Cadence : 1 Gear : 1Speed:1
    Cadence : 1 Gear : 1Speed:1
    MB_Cadence : 1 MB_Gear : 1 MB_Speed :1

    (Follow-ups set to c.l.j.help only, dropping c.l.j.programmer.)

    --
    Eric Sosman
    d
     
    Eric Sosman, Aug 28, 2012
    #2
    1. Advertising

  3. happytoday wrote:

    > I am testing interface but really I can not apply what is explained in
    > oracle tutorials . Could you please explain why I have errors with
    > this code :


    Besides the fact that your example may or may not compile, there are
    some sever issues with the way to design and use classes.

    For better encapsulation, do not directly access fields of an object
    outside of it. You already wrote setters, but you are not using them.
    Also Java best practice is to prefix getters with "get" and setters with
    "set". If you look again into the Java Tutorial, you will see that the
    Bicycle class is actually written that way.

    So Bicycle and its usage would be

    class Bicycle {
    private int speed;
    private int cadence;
    private int gear;

    public int getSpeed() { return speed; }
    public void setSpeed(int speed) { this.speed = speed; }
    // add other getters and setters in the same way
    public void printResults() {
    System.out.println("Bike: [" + speed + ", " + cadence +
    ", " + gear + "]");
    }
    }

    Bicycle bike1 = new Bicycle();
    bike1.setSpeed(1);
    // set other data
    bike1.printResults();

    This way, you can later add checks to the setters (i.e. if your bike
    only has 10 gears, you can handle setting it you gear 11 in an
    "intelligent" way).

    Also, there is no need to redeclare fields of a class that you inherit
    from. Since speed, cadence and gear are already declared for Bicycle,
    you do not need to redeclare them for MountainBicycle:

    class MountainBicycle extends Bicycle {
    public void printResults() {
    System.out.println("MountainBike: [" + speed + ", " + cadence +
    ", " + gear + "]");
    }
    }
    Bicycle bike2 = new MountainBicycle();
    bike2.setSpeed(1);
    // set other data
    bike2.printResults();


    BTW: Another good practice is instead of your printResults() method to
    declare a method called toString() that converts the data to a String
    and then use System.out.println from the caller:
    class Bicycle() {
    // ...
    public String toString() {
    return "Bike: [" + speed + ", " + cadence +
    ", " + gear + "]";
    }
    }
    Bicycle bike1 = new Bicycle();
    System.out.println(bike1.toString());

    This allows more flexibility. For example, if you decide to write the
    data to stderr instead of stdout, you only need to change the calling
    code to
    System.err.println(bike1.toString());
     
    Jens Vonderheide, Aug 29, 2012
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wildepiet
    Replies:
    0
    Views:
    1,862
    Wildepiet
    Jun 14, 2004
  2. cyberco
    Replies:
    8
    Views:
    490
    cyberco
    Feb 25, 2006
  3. Xah Lee
    Replies:
    15
    Views:
    586
    Sherm Pendley
    Mar 23, 2007
  4. Xah Lee
    Replies:
    15
    Views:
    632
    Sherm Pendley
    Mar 23, 2007
  5. Replies:
    4
    Views:
    349
    timjowers
    Nov 2, 2007
Loading...

Share This Page