Which do you like better, "implementations Runable" or "extends Thread"?

Asked 1 years ago, Updated 1 years ago, 90 views

I was looking at how to create threads in Java, and there were two ways: Runnable and Thread!

    public class ThreadA implements Runnable {
        public void run() {
            //Code
        }
    }
    //Started with a "new Thread(threadA).start()" call

This is a runnable example

    public class ThreadB extends Thread {
        public ThreadB() {
            super("ThreadB");
        }
        public void run() {
            //Code
        }
    }
    //Started with a "threadB.start()" call

This is Thread, but is there a difference between the two methods?

multithreading java

2022-09-21 20:02

1 Answers

It's the difference between implementations and extensions, but Java doesn't have multiple inheritance. In the example above, if ThreadB inherits a class called A, extensionsThread is not possible, and then implementationsRunable is used. In that sense, I use implementations Runnable more.

The difference between extensions and implementations is http://gdthink.blogspot.kr/2006/06/extends%EC%99%80-implements%EC%9D%98-%EC%B0%A8%EC%9D%B4.html First of all, extends are used for inheriting normal classes and abstract classes, and implementation is used for interface inheritance.

So, what is inheritance? If my parents are rich, if I inherit from them, I will be rich. Eventually, you can think of it as borrowing their function from something else. There are two forms of inheritance in Java. One is extensions, and the other is implementations, and the first is pure inheritance, and as I explained earlier, it brings all the power and wealth and ability from the parents. The other is called implementation inheritance. Implementation Inheritance ==> Interface is commonly referred to. rather than inheritance I'm just getting the interface. Simply put, you get an inheritance from something, but the inheritance means that it's empty, and I have to fill it up. [Source 2] You can think of childclass2 as inheriting TestInterface and implementing it.

In other words, the implementation doesn't get the fish that the advisor catches, but the way that the advisor catches the fish.

So let's take this as an example. Someone asked me to make a module that saves user input as a file. If the file storage format should be able to be stored in three formats: 1) Excel, 2) html, and 3) Text, the developer will create three different modules because the three functions are different. However, from the point of view of using this module, we would like all three modules to provide the same interface. Eventually, from a Java class perspective, different modules will have to inherit implementation from one interface. Then, the user only needs to look at that one interface and code it.

To make it easier, Extends: Inheritance from parents, implementations: Inheritance from advisors.

Every child in the world has only one parent. Eventually, using extensions, Child can inherit from only one parent. So, what about the advisor? You can meet many advisors to live in the world. They only teach you how to fish. It's implementation inheritance that they practice the way they advise. Eventually, using implementations, Child can meet several advisors. And the advisor is not in a position to give anything to Child Window, so everyone should be in interface class. So there's only a way, and there's no content.

public interface TestInterface {
public static int num = 8;
public void func1();
public void func2();
}
public class Test {
public void func2() {
System.out.println("func2");
}
}
class childclass1 extends Test{

public void func1() {
System.out.println("fun1.");
}

}

class childclass2 implements TestInterface {
public void func1() {
System.out.println("Class Test1");
}
public void func2() {
System.out.println("Class Test2");
}
}


2022-09-21 20:02

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.