File tree Expand file tree Collapse file tree
Interface and Abstract Class Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,13 +12,26 @@ public static void main(String[] args)
1212 Vehicle bike = new Vehicle ();
1313 bike .numberOfWheels = 2 ;
1414 bike .setSpeed (100 );
15+
1516 System .out .println ("Number of wheels on a bike is: " + bike .numberOfWheels );
17+
1618 System .out .println ("Speed of bike is: " + bike .theSpeed );
1719
20+ System .out .println (bike .hashCode ()); //displays the hash code of the objet bike
21+
22+ System .out .println (bike .getClass ());//gets the class of the object
23+
24+ System .out .println (bike .getClass ().getSuperclass ()); //gets the super class of the object (Abstract Class)
25+
1826 /**Use of Abstract Class methods */
1927 car .setCarStrength (100 );
2028 System .out .println ("The strength of car is: " + car .carStrength );
2129
30+ /**New Object */
31+ Object superCar = new Vehicle ();
32+ System .out .println (((Vehicle )superCar ).getSpeed ()); //casting
33+
34+
2235
2336 }
2437}
Original file line number Diff line number Diff line change 11/**abstract class goes before Interface that's why we write extends Crashable before implements Interface */
22public class Vehicle extends Crashable implements Drivable {
3-
43 int numberOfWheels = 4 ;
54 double theSpeed = 0 ;
65 int carStrength = 0 ;
Original file line number Diff line number Diff line change 1+ class RunThread {
2+ public static void main (String [] args ) {
3+ ThreadDemo x = new ThreadDemo ("Thread 1" );
4+ x .start ();
5+
6+ ThreadDemo y = new ThreadDemo ("Thread 2" );
7+ y .start ();
8+ }
9+ }
Original file line number Diff line number Diff line change 1+ class ThreadDemo extends Thread {
2+ String name ;
3+ ThreadDemo (String n ){
4+ name = n ;
5+ System .out .println ("Creating: " +name );
6+ }
7+
8+ public void run (){
9+ System .out .println ("Running: " + name );
10+
11+ try {
12+ for (int i = 4 ; i > 0 ; i --){
13+ System .out .println ("Thread: " +name +" Printing: " + i );
14+ Thread .sleep (500 );
15+ }
16+ }
17+ catch (InterruptedException e )
18+ {
19+ System .out .println ("Thread " + name + " interupted!" );
20+ }
21+ System .out .println ("Thread " +name +" exiting" );
22+ }
23+
24+ }
25+
26+
Original file line number Diff line number Diff line change 1+ # Threads
2+ * It is used for asynchronous behaviour.
3+ * Thread can be created using ** Runnable Interface** or ** Extending Thread**
4+ * Thread.sleep(mills) pauses the current thread execution for defined ** millseconds** .
5+ * We can also use Thread.sleep(mills,nano) which indicates the time in millisecond and nanosecond.
You can’t perform that action at this time.
0 commit comments