Tuesday 1 September 2015

Java Tutorial : What is an Interface (Bike)


Click here to watch in Youtube :
https://www.youtube.com/watch?v=3F9WY1Xm6P8&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge
Java Tutorial : What is an Interface (Bike) 
Java Tutorial : What is an Interface (Bike) 
Java Tutorial : What is an Interface (Bike) 
Bike.java
/**
 * An interface is a group of related methods with empty bodies.
 */
public interface Bike
{
    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);
}
SportBike.java
/**
 * 
 * To implement Bike interface, the name of your class would change (to a
 * particular brand of Bike, for example, such as SportBike)
 *
 */

public class SportBike implements Bike
{

    int speed = 0;
    int gear = 1;

    /*
     * The compiler will now require that methods changeGear, speedUp, and
     * applyBrakes all be implemented. Compilation will fail if those methods
     * are missing from this class.
     */

    @Override
    public void changeGear(int newValue)
    {
        gear = newValue;

    }

    @Override
    public void speedUp(int increment)
    {
        speed = speed + increment;
    }

    @Override
    public void applyBrakes(int decrement)
    {
        speed = speed - decrement;
    }

    public void printStates()
    {
        System.out
                .println("SportBike [speed=" + speed + ", gear=" + gear + "]");
    }

}
InterfaceDemo.java
public class InterfaceDemo
{

    public static void main(String[] args)
    {
        SportBike sportBike = new SportBike();
        sportBike.changeGear(3);
        sportBike.speedUp(60);

        sportBike.printStates();

        sportBike.applyBrakes(30);

        sportBike.printStates();

    }
}
Output
SportBike [speed=60, gear=3]
SportBike [speed=30, gear=3]
To Download InterfaceDemoBikeApp Project Click the below link
https://sites.google.com/site/javaee4321/java/InterfaceDemoBikeApp.zip?attredirects=0&d=1
See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • No comments:

    Post a Comment