Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Creating a Thread

Creating a Thread

Scheduled Pinned Locked Moved Java
questionjavatestingbeta-testing
7 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    Neo10101
    wrote on last edited by
    #1

    Consider the following code:

    public class MyThread extends Thread {
    @Override
    public void run(){
    System.out.println("Testing thread.");
    }
    }

    public class App
    {
    public static void main( String[] args )
    {
    MyThread thread = new Thread();
    }
    }

    Why does the syntax checker not accept line 5 in 'App.java' (The one with the MyThread Reference Type)? If MyThread is aThread, then why is it incorrect to create a new Thread out of it? I know I'm supposed to replace it with new MyThread, but why is Thread not allowed? Doesn't 'extend' mean 'inherits all properties etc.'?

    J L P G 4 Replies Last reply
    0
    • N Neo10101

      Consider the following code:

      public class MyThread extends Thread {
      @Override
      public void run(){
      System.out.println("Testing thread.");
      }
      }

      public class App
      {
      public static void main( String[] args )
      {
      MyThread thread = new Thread();
      }
      }

      Why does the syntax checker not accept line 5 in 'App.java' (The one with the MyThread Reference Type)? If MyThread is aThread, then why is it incorrect to create a new Thread out of it? I know I'm supposed to replace it with new MyThread, but why is Thread not allowed? Doesn't 'extend' mean 'inherits all properties etc.'?

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      CsTreval wrote:

      If MyThread is aThread, then why is it incorrect to create a new Thread out of it?

      Your code is stating that 'Thread' is a 'MyThread' which it isn't. The correct code would be.. Thread thread = new MyThread();

      1 Reply Last reply
      0
      • N Neo10101

        Consider the following code:

        public class MyThread extends Thread {
        @Override
        public void run(){
        System.out.println("Testing thread.");
        }
        }

        public class App
        {
        public static void main( String[] args )
        {
        MyThread thread = new Thread();
        }
        }

        Why does the syntax checker not accept line 5 in 'App.java' (The one with the MyThread Reference Type)? If MyThread is aThread, then why is it incorrect to create a new Thread out of it? I know I'm supposed to replace it with new MyThread, but why is Thread not allowed? Doesn't 'extend' mean 'inherits all properties etc.'?

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You should be creating the same type as you have declared thus:

            MyThread thread = new MyThread();
        

        One of these days I'm going to think of a really clever signature.

        N 1 Reply Last reply
        0
        • L Lost User

          You should be creating the same type as you have declared thus:

              MyThread thread = new MyThread();
          

          One of these days I'm going to think of a really clever signature.

          N Offline
          N Offline
          Neo10101
          wrote on last edited by
          #4

          Yes, but there is also polymorphism as jschell said: Thread thread = new MyThread. I just got the assignment backwards. To me it is more logical to think of a=b instead b=a.

          T 1 Reply Last reply
          0
          • N Neo10101

            Consider the following code:

            public class MyThread extends Thread {
            @Override
            public void run(){
            System.out.println("Testing thread.");
            }
            }

            public class App
            {
            public static void main( String[] args )
            {
            MyThread thread = new Thread();
            }
            }

            Why does the syntax checker not accept line 5 in 'App.java' (The one with the MyThread Reference Type)? If MyThread is aThread, then why is it incorrect to create a new Thread out of it? I know I'm supposed to replace it with new MyThread, but why is Thread not allowed? Doesn't 'extend' mean 'inherits all properties etc.'?

            P Offline
            P Offline
            Peter_in_2780
            wrote on last edited by
            #5

            You've got the idea now, but consider this as another way of looking at it: a MyThread is a Thread and then some. If you had added extra class members - code or data - in the definition of MyThread, it would have stood out like the proverbial. Another clue is in Java's use of the keyword extends. Cheers, Peter

            Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012

            1 Reply Last reply
            0
            • N Neo10101

              Yes, but there is also polymorphism as jschell said: Thread thread = new MyThread. I just got the assignment backwards. To me it is more logical to think of a=b instead b=a.

              T Offline
              T Offline
              TorstenH
              wrote on last edited by
              #6

              Thread thread = new MyThread(); ...works, cause Thread is the parental to MyThread. Using that leaves you with the basic functionality that is given in Thread (unless you cast Thread to a MyThread, then you'll be able to access the additional functions). You could add additional functionality to any Object when you extend it - which is the basic meaning of extension. So when initializing the Object you will have to choose the right one - the parental Object does not own the additional methods, does not know them. You would not be able to access them. Example:

              public class Vehicle{
              // some basic functions, not important here
              }

              public class Bus extends Vehicle{

              public int getSeatQuantity(){
              // return number of seats
              }
              }

              A vehicle can be anything from a bike up to a race car, truck, even a train. It's not important for a bicycle or a race car how many seats it has. But it's important for a bus. So when you want a Bus (...and later probably know how much seats are available) you will have to create a Bus and not just a Vehicle. Simple said: it's more specific.

              regards Torsten When I'm not working

              1 Reply Last reply
              0
              • N Neo10101

                Consider the following code:

                public class MyThread extends Thread {
                @Override
                public void run(){
                System.out.println("Testing thread.");
                }
                }

                public class App
                {
                public static void main( String[] args )
                {
                MyThread thread = new Thread();
                }
                }

                Why does the syntax checker not accept line 5 in 'App.java' (The one with the MyThread Reference Type)? If MyThread is aThread, then why is it incorrect to create a new Thread out of it? I know I'm supposed to replace it with new MyThread, but why is Thread not allowed? Doesn't 'extend' mean 'inherits all properties etc.'?

                G Offline
                G Offline
                Gowtham Gutha
                wrote on last edited by
                #7

                A super class reference variable has the capability to hold its sub class object but a sub class does not have that capability. As MyThread is subclass of Thread, you can write Thread t=new MyThread(); But not MyThread m=new Thread(); That's false.

                Gowtham Gutha (Java-Demos.Blogspot.com)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups