Answer the question
In order to leave comments, you need to log in
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
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();
}
interface Runnable{
void run();
}
interface MyRunnable extends Runnable{
void some(); //Обратите внимание что тут уже run нельзя объявить т.к. он объявлен в Runnable
}
class CoolRunnable implements MyRunnable{
void some(){System.out.print("some from MyRunnable")}
void run(){System.out.print("run from Runnable")}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question