V
V
valomi2020-06-07 19:30:07
Java
valomi, 2020-06-07 19:30:07

How are interfaces implemented in Java?

I understand why we need interfaces and all that. But I have a question: when we implement, for example, Runnable, we simply put our code in the run () method. This is all clear. We are redefining it. But how does Java understand that this method should be put into the thread? For example, if I create my interface:
interface MyRunnable{
void run(); }
will this be the same as what I need for Runnable. I doubt my code will work. So the Java standard library works differently? That interface "does the job", but mine only forces an empty method to be overridden. Those. My interface will not understand to start up a method in a new flow. My guess is that the JRE has additional mechanisms that handle its interfaces differently. Runnable is just an example, you can use any other interface example from the standard library.
Once again: I impliment the method from the interface, put my code into it, and the interface already processes it: it lets it into the stream, creates a button, etc. Here's how it happens? Is this already built into Java itself and not particularly worried about it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Alexandrov, 2020-06-07
@valomi

Apparently you don't understand enough =)
When you implement an interface, for example Runnable, you implement the run() method. You don't redefine it, you implement it, it's not the same with redefinition. In JAVA, there is a Thread class in which it is declared that it knows the Runnable interface (quite specific) and that it definitely has an implementation of the run () method, it does not know other methods that are not declared in the interface.
For example, if you create your interface:

interface MyRunnable{
      void run(); 
}

Then the Thread class will not know what it is and what to do with it and will send you on a fun walk.
Even if you create a "namesake":
interface Runnable{
      void run(); 
}

Then the Thread class will not know what it is and what to do with it and will send you on a fun walk. Because it's not an implementation of java.lang.runnable.
But this one will work:
interface MyRunnable extends Runnable{
      void some(); //Обратите внимание что тут уже run нельзя объявить т.к. он объявлен в  Runnable
}

and when implementing this interface, you will be forced to implement 2 methods already
class CoolRunnable implements MyRunnable{
      void some(){System.out.print("some from MyRunnable")} 
      void run(){System.out.print("run from Runnable")} 
}

And in this case, the Thread class will see that the CoolRunnable class implements the Runnable interface in which there is a run () method that it knows about and needs. It will not see the some() method. When run accordingly, it will only output "run from Runnable".
If you try to explain on your fingers, so to speak, by analogy with something. Let's say you're a class that can click the left mouse button. Nothing else was explained to you. Those. you were given a description that it is called a mouse and that there is a button on the left that you need to press. Now any manufacturer takes this description and implements a mouse with a left button. Each one has its own, from different materials, with different button mechanisms and principles of operation, different shapes, and even with a bunch of other buttons. All of them get to you, you see that a particular instance of the mouse is implemented according to the description that you know, in addition, this is exactly the description that you have and not the one of the same name. Based on this, you accept that this thing suits you and start pressing the left mouse button on any such mouse that you will be given. If another object comes, then you break your finger,

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question