Thursday , October 17 2019

JAVA #26 Interfaces in Java


Interface in Java

As you’ve already learned, objects define their interaction with the outside world through the methods that they expose. Methods form the object’s interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the “power” button to turn the television on and off.

In its most common form, an interface is a group of related methods with empty bodies. A bicycle’s behavior, if specified as an interface, might appear as follows:

interface Bicycle {

    //  wheel revolutions per minute
    void changeCadence(int newValue);

    void changeGear(int newValue);

    void speedUp(int increment);

    void applyBrakes(int decrement);

}

 

To implement this interface, the name of your class would change and we use the implements keyword in the class declaration:

class ACMEBicycle implements Bicycle {

    void changeCadence(int newValue) {
         ...
    }
    void changeGear(int newValue) {
         ...
    }
    void speedUp(int increment) {
        ...    
    }

    void applyBrakes(int decrement) {
        ...
    }
}

 

// File name must be Intefaces.java
package interfaces;
public class Interfaces implements Shape{

    public static void main(String[] args) {

        Interfaces objint = new Interfaces();
        objint.Rectangle(19, 23);
        objint.Triangle(02, 34, 123);
    }

        @Override
        public void Triangle(int x, int y, int z)
        {
            System.out.println("Area"+(x+y+z));
        }

        @Override
         public void Rectangle(int x,int y)
         {
             System.out.println("Area"+(x+y));
         }
}

 interface Shape
 {
        void Triangle(int x, int y, int z);

        void Rectangle(int x,int y);
 }

 

About Faheem

Faheem
An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #32 – Java application using SQL server (Part: 01)

  In this part we made the GUI front end of a Application.

Leave a Reply

Your email address will not be published.