Rahmadnet

Java Inheritance Asigtment

Mar 5th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.62 KB | None | 0 0
  1. package Inheritance;
  2.  
  3. /* What Is an Interface?
  4.  * As you've already learned, objects define their interaction with the
  5.  * outside world through the methods that they expose. Methods form
  6.  * the object's interface with the outside world; the buttons on the front
  7.  * of your television set, for example, are the interface between you
  8.  * and the electrical wiring on the other side of its plastic casing. You
  9.  * press the "power" button to turn the television on and off.
  10.  *
  11.  *
  12.  * In its most common form, an interface is a group of related methods
  13.  * with empty bodies. A bicycle's behavior, if specified as an interface,
  14.  * might appear as follows:
  15.  */
  16.  
  17.  
  18. interface Bicycle
  19. {
  20.      //  wheel revolutions per minute
  21.     void changeCadence (int newValue);
  22.     void changeGear (int newValue);
  23.     void speedUp (int increment);
  24.     void applyBrakers(int decrement);
  25. }
  26.  
  27.   /*
  28.    * To implement this interface, the name of your class would change (to
  29.    * a particular brand of bicycle, for example, such as ACMEBicycle),
  30.    * and you'd use the implements keyword in the class declaration:
  31.    */
  32.  
  33. class ACMEBicycle implements Bicycle
  34. {
  35.     int cadence = 0;
  36.     int speed = 0;
  37.     int gear = 1;
  38.    
  39.        // The compiler will now require that methods
  40.        // changeCadence, changeGear, speedUp, and applyBrakes
  41.        // all be implemented. Compilation will fail if those
  42.        // methods are missing from this class.
  43.  
  44.     @Override
  45.     public void changeCadence(int newValue)
  46.     {
  47.         cadence = newValue;
  48.     }
  49.  
  50.     @Override
  51.     public void changeGear(int newValue)
  52.     {
  53.         gear = newValue;
  54.     }
  55.  
  56.     @Override
  57.     public void speedUp(int increment)
  58.     {
  59.         speed = speed + increment;
  60.     }
  61.  
  62.     @Override
  63.     public void applyBrakers(int decrement)
  64.     {
  65.         speed = speed - decrement;
  66.     }
  67.    
  68.     void printState ()
  69.     {
  70.         System.out.println("Cadence : " + cadence);
  71.         System.out.println("Speed   : " + speed);
  72.         System.out.println("Gear    : " + gear);
  73.     }
  74. }
  75.  
  76.  
  77.  /*
  78.   * Implementing an interface allows a class to become more
  79.   * formal about the behavior it promises to provide.
  80.   * Interfaces form a contract between the class and the
  81.   * outside world, and this contract is enforced at build
  82.   * time by the compiler. If your class claims to implement
  83.   * an interface, all methods defined by that interface must
  84.   * appear in its source code before the class will
  85.   *  successfully compile
  86.   */
  87.  
  88. public class BicycleDemo
  89. {
  90.     public static void main(String[] args)
  91.     {
  92.         ACMEBicycle ab1 = new ACMEBicycle();
  93.         ACMEBicycle ab2 = new ACMEBicycle();
  94.        
  95.         ab1.changeCadence(50);
  96.         ab1.speedUp(10);
  97.         ab1.speedUp(10);
  98.         ab1.changeGear(2);
  99.         ab1.printState();
  100.     }
  101.        
  102. }
Advertisement
Add Comment
Please, Sign In to add comment