I am studying Java and Thread and have a question about inheritance.
For example I have a parent class called SuperThread
and this thread has LocalThread
and NetThread
.
I would like to use SuperThread
's constructor in LocalThread
and NetThread
.
for example:
public class SuperThread{
Private String name;
Private int size;
SuperThread(String name, int size){
this.name = name;
this.size = size;
}
}
public class LocalThread extends Thread{
String path = "";
LocalThread(String name, int size, String path){
Super(name, size);
this.path = path;
}
public class NetThread extents Thread{
//Share common constructor from parent class, but it was own part
}
}
However, I know that I have to extend Thread
in order to use thread.
Is there any way to extend Threads while inheriting constructor from parent's class ?
Use Runnable
interface and override run
method in your LocalThread
class.
public class LocalThread extends SuperThread implements Runnable{
String path = "";
LocalThread(String name , int size, String path){
super( name, size);
this.path = path;
}
@Override
public void run() {
//some actual job here
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.