Basics Of Threads by ravindu

What is a thread?
There are two ways to create a thread:
  1. By extending Thread class
  2. By implementing Runnable interface.

Thread class provide constructors and methods to create and perform operations on a thread .Thread class extends Object class and implements Runnable interface.

Creating a thread
Thread t = new Thread();


By extending Thread class
The only disadvantage of this way is you can only inherit one class .
Class Downloder extends Threds{
Public void run(){ // always the executing code should be inside the run method
Filedownloder fd= new Filedownloder();
Fd.downlode();
}
}
To invoke the run method we should start the thread
Thread t = new Thread();
t.start(); we should call the start method in order to call the run()




By implementing Runnable interface.
Here the problem of inheritance is overcome.
Class downloader implement runnable{
}
Public void run(){
Filedownloder fd =new Filedownloder(){
Fd.downloder();
}
}
Invoking the run method
downloder d= new downloader();
Thread t=new Thread(d);
t.start();



Abstract Class

The most important cases in java abstraction  
  • Abstract classes may or may not contain abstract methods ie., methods with out body ( public void get(); )
  • But, if a class have at least one abstract method, then the class must be declared abstract.
  • If a class is declared abstract it cannot be instantiated (an object cannot be created ).
  • To use an abstract class you have to inherit it from another class, provide implementations to the abstract methods in it.
  • If you inherit an abstract class you have to provide implementations to all the abstract methods in it.
Final keyword

Final keyword

Final Keyword tutorial Report By:-Ravindu Perera 2014259
Case 1

How to stop a variable from changing?
public class Tutorial {
public static void main(String[] args) {
Test test = new Test();
System.out.println(test.p1);
}
}

class Test {
public final double p1 = 3.14;

Test() {
p1 = 2.14;//due to the finall keyword the intialised value cannot change
}
}

here because the variable made final so we cannot change the variable it is made a constant.





Case 2
How to stop a method from being overriden when it is inherited?
public class finalMethod {

final void data() {
System.out.println("I have 1 GB");
}

}

class tre extends finalMethod {
void data() {//here the method is not being allowed to ovrride
System.out.println("I have 5GB");
}
public static void main(String []args){
}
}
because the method uses the keyword final the extened class will be not allow to overrride the method as final keyword stops it.












Case 3
How to prevent a class from being inherited?
public final class finalMethod {

final void data() {
System.out.println("I have 1 GB");
}

}

class tre extends finalMethod {//the final keyword prevents inheritance
void data() {
System.out.println("I have 5GB");
}
public static void main(String []args){
}
}

in this code the final keyword is being used to prevent the superclass being inherited to the subclass so an error message is being shown.