Interface in Java

Interface itself that you are creating is abstract, and that means that there is no actual code for any of the methods, you only supply the actual method names and the parameters. The idea is to use interface to provide common behavior that can be used several classes by having them all implement the same interface.

It is really a way to standardize the way a particular set of classes is used. 

It is actually a commitment, a contract if you will, that the method’s signatures that those variables in the interface constants if you defined any will not change.

The interface exists to define the methods that must be implemented. So private or public really useless.

How would we go about implementing this interface?

  • Our class is going to be using the interface.
  • To use implements keyword
package anote;

public class Main {
    public static void main(String[] args) {

        ITelephone janePhone;
        janePhone = new DeskPhone(987654321);
        janePhone.powerOn();
        janePhone.callPhone(987654321);
        janePhone.answer();

        System.out.println("----------------------------");
        janePhone = new MobilePhone(123456789);
        janePhone.powerOn();
        janePhone.callPhone(123456789);
        janePhone.answer();
    }
}

interface ITelephone {
    void powerOn();
    void dial(int phoneNumber);
    void answer();
    boolean callPhone(int phoneNumber);
    boolean isRinging();
}

class DeskPhone implements ITelephone{

    private int myNumber;
    private boolean isRinging;

    public DeskPhone(int myNumber) {
        this.myNumber = myNumber;
    }

    @Override
    public void powerOn() {
        System.out.println("power on");
    }

    @Override
    public void dial(int phoneNumber) {
        System.out.println("Now ringing " + phoneNumber + "on deskphone");
    }
    @Override
    public void answer() {
        if(isRinging){
            System.out.println("Answering the desk phone");
            isRinging = false;
        }
    }

    @Override
    public boolean callPhone(int phoneNumber) {
        if (phoneNumber == myNumber){
            isRinging = true;
            System.out.println("Ring ring");
        }
        else{
            isRinging = false;
        }
        return isRinging;
    }

    @Override
    public boolean isRinging() {
        return false;
    }
    
}

class MobilePhone implements ITelephone{

    private int myNumber;
    private boolean isRinging;
    private boolean isOn = false;

    public MobilePhone(int myNumber) {
        this.myNumber = myNumber;
    }

    @Override
    public void powerOn() {
        isOn = true;
        System.out.println("Mobile phone powered up");
    }

    @Override
    public void dial(int phoneNumber) {
        if (isOn){
        System.out.println("Now ringing " + phoneNumber + "on mobile phone");
        }
        else{
            System.out.println("Phone is switched off");
        }
    }
    @Override
    public void answer() {
        if(isRinging){
            System.out.println("Answering the mobile phone");
            isRinging = false;
        }
    }

    @Override
    public boolean callPhone(int phoneNumber) {
        if (phoneNumber == myNumber && isOn){
            isRinging = true;
            System.out.println("Melody ring");
        }
        else{
            isRinging = false;
            System.out.println("Mobile phone not on or number different");
        }
        return isRinging;
    }

    @Override
    public boolean isRinging() {
        return false;
    }
}
power on
Ring ring
Answering the desk phone
----------------------------
Mobile phone powered up
Melody ring
Answering the mobile phone

Leave a Reply

Your email address will not be published.

ANOTE.DEV