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