[JAVA] What is an interface?

Good work. I'm new to programming.

Look back on the words that have passed by somehow I would like to post a few things that are difficult to understand or that interest me. I hope it will be helpful for those who are wondering what an "interface" is.

1. What is an interface?

If you say it very clearly

The "promise" and "standard" are "this class has ** methods (≒ functions)!".

Let me give you an example. For example, suppose you have an interface called a control panel. Suppose this control panel has methods called "play", "stop", and "fast forward".

When this controlPanel is implemented

Make sure that the class implements "play", "stop", and "fast forward".
It is also a constraint that an error will occur if it is not implemented.

There is an easy-to-understand diagram below, so please refer to it if you like.

References: https://www.gixo.jp/blog/5159/

2. What exactly?

In summary, the declaration "I promise to implement this function" You should think that.

3. Usage, convenient usage

Is it okay for me to read this and declare it first? Isn't it difficult to expand the function? I thought.

I have a new and easy-to-understand example.

  1. The standard of the outlet has been decided, and it is basically necessary for electrical appliances to make an outlet that conforms to the standard (interface).
  2. I made a hair dryer
  3. I made a microwave
  4. I made a refrigerator
  5. The television was invented

If all the outlets of the above home appliances are unified, you can use the same power strip. There is no need to buy a converter or change the shape of the outlet by performing construction work. To put it the other way around, home appliance developers are forced to "need to unify the shape of outlets," which is the essence of the interface.

Quote source: https://teratail.com/questions/70213

Considering the future as mentioned above, Declare "Make it with such a standard" Isn't the merit very easy to understand?

If the standards are different, we can expect very unfavorable results for usability ...

3. How is it different from extend?

Now, I would like to touch on extend here as well. For example, suppose you have a person class like this:

class Person {
     String name;
     eat(){}
```

 Inherit this person class and create a Programmer class.
 To create a class that inherits from a class, follow the class name
 extends class name
 Just add.

```java
class Programmer extends Person {
     Programming(){}
}
```

 This is the use of a parent class called "inheritance".
 For the use of the interface, here is
 Extends and uses the ** parent method **.

 For example, if you want to use the values and behaviors used in the parent method
 No need to bother to reproduce the behavior of the parent method again
 It can be used by calling (inheriting).

 Please note that this is different from the interface.

 What did you think.
 I hope it will help you understand the interface.


Recommended Posts

What is an interface?
What is an initializer?
What is an operator?
What is an annotation?
What exactly is an API?
[Ruby] What is an instance?
What is Docker?
What is null? ]
What is java
What is Keycloak
What is maven?
What is Jackson?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is params
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is centOS
What is RubyGem?
What is programming?
What is before_action?
What is Docker
What is Byte?
What is Tomcat
What is `docker-compose up`?
What is a constructor?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is Spring Tools 4
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is Java technology?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
What is JPA Auditing?
[Swift] What is dismiss?
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Android] What is Context? ??
[Java] What is ArrayList?
[Ruby] What is true?
[JAVA] What is the difference between interface and abstract? ?? ??
What is object-oriented after all?
What is HttpSession session = request.getSession ();
What is Java Assertion? Summary.