A newbie question about classes

Hi there everybody,

I have a little question:

If I have a class witch extends another class, i.e.:

public class A extends B {

}

The objects I create of A class are type A or type B.

I think I am not explaining myself very well… sorry, English is not my natural tongue.

I want to override the run() method from the Thread class, so I create another class that extends the Thread class

puclic class MyThreadClass extends Thread {

}

So, when I want to create a thread, I have to define the object, but this object must be of type MyThreadClass or Thread?

MyThreadClass myThread = new MyThreadClass();

or

Thread myThread = new MyThreadClass();

I am beguining to read the book “Developing Games in Java (Nrg – Programming)” and the first chapter is about threadings. It says the second option is correct but I am not sure of that…and even I dont understand why.

Again, sorry about my spelling and thanks in advance.

In general the second way is correct. There is a phrase in programming “Program to an interface not an implementation”. It makes it easier to swap out different implementations. This StackOverflow question contains a lot of information.

In short, your code will be more flexible and easier to change with the second approach. For instance, if you end up creating a “MyThread2” class and decide that myThread should be an instance of that, all you need to do is to change the code where it’s constructed.

This is not recommended since you now cannot extend another class. For example say, run a thread inside a JFrame. So it’s recommended way is


public class MyThreadClass implements Runnable
{

    public MyThreadClass()
    {
        new Thread(this).start();  // you may want to write this in a custom start method.
    }

    public void run()
    {
        // Do after the thread starts.
    }

}

Now you can extend other classes.

It really depends. If you are absolutely certain that your thread object is going to be your own thread class then by all means, go with it. But if you want to have it so that the thread object is different based on different conditions then you can go with the first one.

The only difference really is how ‘broad’ your definition is. The first one really just means: “Let’s define a thread object that is an object of MyThreadClass()”, when trying access the members of this instance, you’ll see that your ‘MyThreadClass’ field members and methods’ are not there. Because Java still treats that thread object as ‘a general thread’ but not an object of ‘MyThreadClass’

If you want to access members of ‘MyThreadClass’ using your first method, then you might have to do something like this:


if(myThread instanceof MyThreadClass) {
  ((MyThreadClass) myThread).myThreadClassMethods(); //Using casting
}

If you go with the second method, then its like saying: “Ok let us make a MyThreadClass object that is created from MyThreadClass”, in this case, object contains all the unique fields and methods from MyThreadClass as well as Thread. This is very handy if you are working with thread objects of type of ONLY MyThreadClass

I hope that doesn’t confuse you :slight_smile: