What is interfaces in java programming language? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

What is interfaces in java programming language?

Java programming

29th Jul 2018, 11:02 AM
Kirtikumar
Kirtikumar - avatar
3 Answers
+ 2
An interface is a completely abstract class. To inherit from it, you'll have to inplement it in your class.
29th Jul 2018, 11:04 AM
Jonas Schröter
Jonas Schröter - avatar
0
The interface is an agreement with other developers on the signatures of your functions. Imagine a big machine that has a physical interface of three buttons (methods). You do not have to know what's inside this car. In addition, you can use any machine that has the same interface In the interface, you write the signatures of functions that the class implementing it must have. And you can work with this class as an interface. Example: InterfaceName interf = (InterfaceName) someObjectImplementsThisInterface; // upcast interf.someMethod(); === With interface you can do multiple inheritance and upcast your class to any interface it implements
29th Jul 2018, 11:11 AM
Mishin870
Mishin870 - avatar
0
I like metaphors. :) Thanks to Mishin870 for the idea: Interfaces are useful because they facilitate abstraction (a way of defining things in a fuzzy way without having a concrete implementation). Here is why abstraction is useful: You can DEFINITELY send an email, text message, or make a phone call from your phone by clicking buttons on your phone/computer. You don't have to know what's inside the computer, how it works, or how it moves data around. You just know what the buttons do, and you let the computer do the rest. :) Writing an interface is just like writing a contract (an agreement between two parties). An interface's contract says something like "Any class or struct that derives from this interface must create implementations for all of my defined members (methods, properties, events, and indexers)". An interface is like a box of buttons that you can attach to a class/struct. All the buttons (members) defined in the interface say something like "I am a button. If you press me, I require an input of 'x' type and will produce some output of 'x' type". But the button isn't connected to anything yet, so it can't DO anything yet. The only way to have it DO anything is to write a class/struct that will act like a sort of 'machine' that will be attached to those buttons. The implementation is inside the machine (class) but the buttons to press and the definitions of what those buttons DO (members) were defined by the interface. ANY class/struct can attach any number of 'button boxes' (interfaces), however, MUST provide an implementation for each member (button) that it inherits.
5th Aug 2018, 5:23 AM
Justin Kindrix
Justin Kindrix - avatar