[Java] Bicycle – Pastebin.com

Dovie Salais

Not a member of Pastebin yet? Sign Up, it unlocks many cool features! // Java program to illustrate the // concept of inheritance // from G4G   // base class class Bicycle {     // the Bicycle class has two fields     public int gear;     public […]

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. // derived class

  40. class MountainBike extends Bicycle

  41. {

  42.    

  43.     // the MountainBike subclass adds one more field

  44.     public int seatHeight;

  45.  

  46.     // the MountainBike subclass has one constructor

  47.     public MountainBike(int gear,int speed,

  48.                         int startHeight)

  49.     {

  50.         // invoking base-class(Bicycle) constructor

  51.         super(gear, speed);

  52.         seatHeight = startHeight;

  53.     }

  54.        

  55.     // the MountainBike subclass adds one more method

  56.     public void setHeight(int newValue)

  57.     {

  58.         seatHeight = newValue;

  59.     }

  60.    

  61.     // overriding toString() method

  62.     // of Bicycle to print more info

  63.     @Override

  64.     public String toString()

  65.     {

  66.         return (super.toString()+

  67.                 nseat height is “+seatHeight);

  68.     }

  69.    

  70. }

  71.  

  72. // driver class

  73. public class Test

  74. {

  75.     public static void main(String args[])

  76.     {

  77.        

  78.         MountainBike mb = new MountainBike(3, 100, 25);

  79.         System.out.println(mb.toString());

  80.            

  81.     }

  82. }

We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand

Source Article

Next Post

Used Car Dealer Near Brick NJ

We understand that buying a used car can be a stressful process that requires careful consideration. Our goal here at Automotive Avenues is to save you time and money, while providing you with the resources necessary to make a sound decision on your next vehicle purchase. It’s easy to see […]

Subscribe US Now