Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does an interface in Java interface with?

I've been trying to learn some basic object oriented programming in Java. I was curious to know what the origin of the word interface is , if there is any documented description. Also I was trying to make sense of what it means by thinking of a generic concept as

A point where two systems, subjects, organizations, etc. meet and interact

I got this definition from google search. What are the two systems/entities that are interfacing? Or maybe my analogy used is inappropriate?. So far I think of it as a skeleton to define methods and property outlines.

like image 718
Parapan Avatar asked Feb 12 '23 10:02

Parapan


2 Answers

Software interfaces are one-way (though there are ways to pass the calling object as a reference to the callee), unlike Electrical connectors that interface both ways directly.

If you accept that difference in definition then the object 'implementing' the interface, is the object to be interfaced with. it allows other objects to connect to it using a well defined set of methods.

To compare it further to electronics, if 3 different types of devices all support audio-jacks, then all 3 devices essentially state: you can listen to me, I play audio. They could be very different devices (mp3 player, sonar, geiger counter) but they all clearly state: if you plug in a headphone, you can get sound out of me.

This is what an interface does in software. it states: I provide feature X, no matter what actual component I am.

so anything that implements the Map interface, can have .get(...) and .values() and .keySet() called on it. Anything that implements an AudioStream interface will yield an audiostream when called.

The object interfacing with the object supplying the interface can interact with this object in a predefined and well documented way. Ofcourse, how the object providing the interface actually makes it work can be completely different.

like image 167
Joeblade Avatar answered Feb 14 '23 01:02

Joeblade


An interface defines the common interface (hence the name :) multiple objects (of potentially different types) have in terms of method signatures. This is for consumption by other objects.

The JLS Chapter 9 states:

An interface declaration introduces a new reference type whose members are classes, interfaces, constants, and methods. This type has no instance variables, and typically declares one or more abstract methods; otherwise unrelated classes can implement the interface by providing implementations for its abstract methods. Interfaces may not be directly instantiated.

So this is more of a definition in terms of behavior than etymology.

like image 30
eckes Avatar answered Feb 14 '23 01:02

eckes