Hi, I have a working program and I need to answer the following questions:
1:where was polymorphism was used in this application?
2:identify where overriding or overloading was used in this application?

I belive overriding is used by the Runnable method to override the run() method. Would that be a good explenation? I am not sure how to clearly communicate how polymorphism is used. Any ideas on how to explain it?

Thanks

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package treading;

/**
 *
 * @author Q
 */
//Runnable is the name of the interface, to use the interface we use the implement keyword. 
public class Treading implements Runnable {
//Thread object is to execute a single method and to execute it just once.
//The method is executed in its own thread of control, which can run in parallel with other threads
    private int cDown = 5;
    private static int cThread = 6;
    private int threadNum = --cThread;
//constructs opbject that will exicute in a different thread.
    /**
     *
     */
    public Treading() {
        System.out.println("\nStarting thread number => " + cThread + "\n");
    }
//Overriding was done by the Runnable interface superseeding the previous definition of run()
    @Override
    //This run() method defines the task that will be performed by the thread.
//This will be called by the start() method.
    public void run() {
        while (true) {

            System.out.println(
                    "  Thread:  "
                    + threadNum
                    + " ( Countdown = " + cDown + " )");

            for (int j = 0; j < 5; j++) {
            }

            if (--cDown == 0) {
                System.out.println(
                        "\nBLAST OFF!" + "\n");
                return;
            }
        }
    }
//Start() method creates the new thread of control executing the Thread object’s run() method.
//Starts all 5 threads.
    private static void Countdown()
            throws java.lang.InterruptedException {

        for (int i = 0; i < 5; i++) {
            Thread.sleep(1000);
            Runnable start;
                    start = new Treading();
            Thread counting = new Thread(start);
            counting.start();
        }

    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
            throws java.lang.InterruptedException {
        Countdown();


    }
}

Recommended Answers

All 2 Replies

1:where was polymorphism was used in this application?

The definition of polymophism is here. Look at the last sentence of the first paragraph...

2:identify where overriding or overloading was used in this application?

The meaning of override explains itself (replace something that is already exists). For overload, it is similar to add on. So look for the code portion that has the feature (and you are on the right track already).

Oh I see overriding is a polymorphic function. Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.