Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  A newbie question about classes  (Read 257 times)
0 Members and 1 Guest are viewing this topic.
Offline Worso

Innocent Bystander





« Posted 2013-05-29 11:59:34 »

Hi there everybody,

I have a little question:

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

1  
2  
3  
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

1  
2  
3  
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?

1  
MyThreadClass myThread = new MyThreadClass();


or

1  
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.

Offline actual

JGO Coder


Medals: 24



« Reply #1 - Posted 2013-05-29 12:14:06 »

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.
Offline SHC
« Reply #2 - Posted 2013-05-29 12:20:13 »

Quote
1  
2  
3  
4  
public class MyThreadClass extends Thread
{

}


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

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
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.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mrdeathjockey

Senior Member


Medals: 5
Projects: 1
Exp: 3 years


Trying to be a good programmer, step by step


« Reply #3 - Posted 2013-05-29 12:22:00 »

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:
1  
2  
3  
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 Smiley

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (50 views)
2014-04-15 18:08:23

BurntPizza (46 views)
2014-04-15 03:46:01

UprightPath (62 views)
2014-04-14 17:39:50

UprightPath (44 views)
2014-04-14 17:35:47

Porlus (61 views)
2014-04-14 15:48:38

tom_mai78101 (84 views)
2014-04-10 04:04:31

BurntPizza (142 views)
2014-04-08 23:06:04

tom_mai78101 (242 views)
2014-04-05 13:34:39

trollwarrior1 (201 views)
2014-04-04 12:06:45

CJLetsGame (208 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!