Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (616)
Games in Android Showcase (173)
games submitted by our members
Games in WIP (659)
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 433 times)
0 Members and 1 Guest are viewing this topic.
Offline Worso

Innocent Bystander

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

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();


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: 25

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

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

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 Devvie

Medals: 6
Projects: 1
Exp: 3 years

Trying to be a good programmer, step by step

« Reply #3 - Posted 2013-05-29 10: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:
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.

Coldstream24 (19 views)
2015-09-03 00:41:28

Andrew_3ds (31 views)
2015-09-01 19:08:10

afikri (20 views)
2015-08-31 09:30:22

afikri (27 views)
2015-08-31 09:30:07

afikri (15 views)
2015-08-31 09:27:24

afikri (17 views)
2015-08-31 09:26:40

Roquen (32 views)
2015-08-29 11:30:54

GamerC4 (41 views)
2015-08-22 20:38:50

GamerC4 (37 views)
2015-08-22 20:37:18

GamerC4 (43 views)
2015-08-22 20:37:01
HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36

How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30 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‑
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!