0

I'm going through a video about Interfaces in Java, and I understand the concept of it. However, in this video, I do not fully get it the syntaxing of it within the Main Class.

The way I am interpreting this is that the variable timsPhone is declared of type Interface.

Then, timsPhone is instantiated with a new class called DeskPhone. However, I know that can't be the case since Interfaces cannot be instantiated.

I'm guessing this 2 lines of code works since DeskPhone implements ITelephone, but I don't get why we would need write it the way it's been done.

The way I am seeing this is that it would be better to create a new instance of the class DeskPhone, and declare the methods from within that instance, instead of creating a variable for ITelephone

Could someone explain to me in very simple-to-understand what this means, and when we would ever need to declare interfaces as such?

public interface ITelephone {
    boolean isRinging();
}

public class DeskPhone implements ITelephone {

    private int myNumber;
    private boolean isRinging;

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

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

}

public class Main {

    public static void main(String[] args) {
        ITelephone timsPhone; 
        timsPhone = new DeskPhone(123456); // This part I do not understand
    }
}

0 Answers0