Java

[Java] Bicycle – Pastebin.com

Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Java program to illustrate the

  2. // concept of inheritance

  3. // from G4G

  4.  

  5. // base class

  6. class Bicycle

  7. {

  8.     // the Bicycle class has two fields

  9.     public int gear;

  10.     public int speed;

  11.        

  12.     // the Bicycle class has one constructor

  13.     public Bicycle(int gear, int speed)

  14.     {

  15.         this.gear = gear;

  16.         this.speed = speed;

  17.     }

  18.        

  19.     // the Bicycle class has three methods

  20.     public void applyBrake(int decrement)

  21.     {

  22.         speed -= decrement;

  23.     }

  24.        

  25.     public void speedUp(int increment)

  26.     {

  27.         speed += increment;

  28.     }

  29.    

  30.     // toString() method to print info of Bicycle

  31.     public String toString()

  32.     {

  33.         return(“No of gears are “+gear

  34.                 +n

  35.                 + “speed of bicycle is “+speed);

  36.     }

  37. }

  38.  

  39. //

Read More