Friday, 17 August 2012

What is multithreading in Java ?

A multithreading program contains two or more parts that can run concurrently. Each part of such a program   is called a thread and each thread defines a seperate path of execution. Thus multithreading is a specialized form of multitasking.

However there are two distinct types of multitasking :
1. Process based       
2. Thread based

It is important to understand the difference between these two : 
Process based multitasking is the feature that allows your compiler to run two or more programs concurrently. Example. Process based multitasking enables you to run the Java compiler at the same time when you are using a text editor.
In thread based multitasking, a single program can perform two or more tasks simultaneously.

Thread Priorities :
Java assigns to each thread a priority that determines how that thread should be treated with respect to the others. A thread priority is used to decide when to switch from one running thread to the next. This is called the context switch.
  •  A thread can voluntarily relinquish control. This is done by explicitly locking or sleeping on pending input output. In this scenario all other threads are examined and the highest priority thread that is ready to run is given the CPU.
  • A thread can be preemted by a higher priority thread.

Java defines two ways of creating a thread :
  1. You can implement runnable interface .
  2. You can extend the thread class itself.

Now follow :


Wednesday, 15 August 2012

Uses of keyword final in JAVA



Keyword " final " has three uses :

Use 1 : A variable can be declared as final, doing so prevents its contents from being modified. This means that we must initialize a final variable when it is declared.
For ex :
Final int a = 2 ;


The next two uses of Final apply to inheritance. 


Use 2 : Using Final to prevent overriding

While method overriding is one of the Java's most powerful feature, there will be times when we want to prevent it from occurring, to disallow a method from being overridden specify Final as a modifier at the start of its declaration.
Methods declared as final cannot be overridden.

class A
{      Final void math( )
        {
              System.out.println("This is a final method");
         }
}
class B extends A
{        void math( )         //illegal
          {       
                System.out.println("illegal"); 
           }
}



Use 3 : Using Final to prevent inheritance

Sometimes we want to prevent a class from being inherited. To do this precede the class declaration with final. Declaring a class as final implicitly declares all of its methods as final too.
As we expect it is illegal to declare a class as both abstract and final since an abstract class is incomplete by itself and relies upon its subclasses to provide complete implementations.

final class A
{    ......
      ........
      ........
}
class B extends A                    // illegal
{ 
}



Hope this helped.....

Use of keyword super in JAVA



The keyword super has two general forms :

The first calls the constructor of the super class.
The second is used access a member of the super class that has been hidden by member of a sub class.

First use of super

class Box
{    double width;
      double height;
      double depth;
      Box(double w, double h, double d)            //constructor
         {       width = w ;
               height = h ;
               depth = d ;
          }
      double volume( )
        {         return width*height*depth;
       }

 class Boxweight extends Box
 {       
        double weight;
        Boxweight( double w, double h, double d, double m)
        {
                   super(w, h, d);    //This will call the constructor of  
                                               //super class Box
                   weight = m;
         }
  }



Second use of super

This 2nd form of super is most applicable to situations in which member names of a subclass hide members by the same name in the   
super class.

class A
{      int i;
}

class B extends A
{      int i;
        B( int a, int b)
        {     super.i = a;
               i = b;
         }
         void show ( )
         {     
                 System.out.println(" i in superclass : " +super.i  );       
                                                                           //will
                                                                           // print the value of i
                                                                           // of super class A 
                 System.out.println(" i in subclass : " +i )  ;
          }
  }

class UseSuper
{ 
          public static void main( String args[] )
          {
                  B Subob = new B(1,2);
                  Subob.show( );
           }
 }

       
Hence the output of the above program will be:

i in superclass : 1
i in subclass : 2



please ask if there are any queries....

Tuesday, 14 August 2012

Interesting Full forms of various Brands and other things. Often asked in General knowledge in PCS and other exams.



Expansion of :


ADIDAS - All Day I Dream About Sports
( However its named after the founder Adolph Dassler from his nikname, Adi for Adolph and Das for Dassler. But still this is the commonly known full form of Adidas)


FILA - Finally I Left Adidas
( Its said that the Founder of Fila first worked with adidas but when he left adidas, he started this company)


NIKE - Greek goddess of victory


KISS - Keep It Short and Simple
(In mode of communication)


LG - Life's Good (in general)
( But its " Lucky Goldstar " as the company was started in 1947 as lucky goldstar and in 1950 it abbreviated its name to LG)


KFC - Kentucky Fried Chicken


LOVE - Long Original Valuable Emotion
( Different people interpret the word in different ways but this is the most common full form)

ICICI -  Industrial Credit And Investment Corporation Of India.


BMW - Bayerische Motoren Werke (in German) and Bavarian Motor Works (in English)



Also have a look at full forms of famous companies.

Friday, 10 August 2012

Important questions in technical interview of Java.



Here I am providing the most important questions that are mostly asked in any technical interview of Java :

1. (If you have also mentioned C and C++ in your resume)
     What is the difference between C, C++ and Java ?


2. Explain OOPs concepts with example.


3. What is the difference between method overloading and method overriding?


4. What is runtime and compile time polymorphism ? Explain with example.

5.  Difference between String and StringBuffer class ?

6.  Multithreading and runnable interface.

7.  Meaning of public, static, void in " public static void main( String args[]) " 


8. What is a constructor?


9. What is the use of keyword super in Java?


10. What is the use of keyword final in Java?




Will provide some more interview questions and answers in my future updates.........

Till then practice these...........

Good Luck.........

Parameterized constructors


class Box
    {    double width;
          double height;
          double depth;

          Box( double w, double h, double d)
          {    System.out.println("constructing box");
                width=w;
                height=h;
                depth=d;
         }

         double volume( )
         {       
                   return width*height*depth;
         }
     }
class BoxDemo
     {
               public static void main( String args[])
               {    Box mybox1=new Box(10,20,15);
                     Box mybox2=new Box(3,6,9);
                     double vol;
                     vol=mybox1.volume( );
                     System.out.println("Volume of first box is"+vol);
                     vol=mybox2.volume( );
                     System.out.println("Volume of second box is"+vol);
                 }
      }


In a parameterised constructor the parameters are passed while creating a object as done here:
Box mybox1=new Box(10,20,15);
The values 10,20 and 15 are passed to the constructor of class box and width, height and depth of box 1 will be initialized to these values.
Similarly we are passing values 3,6 and 9 for box 2, hence its width,height and depth are initialized to 3, 6 and 9.

hence the output of above program will be:

Volume of first box is3000
Volume of second box is162            




To know about unparameterized constructors click here

What is a constructor in Java?



It can be tedious to initialize all of the variables in a class each time an instance is created. JAVA allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of constructor.
A constructor initializes an object immediately upon creation. It has the same name as the class in which it resides and is syntactically similar to a method. Once defined it is automatically called immediately after the object is created before the new operator completes.
Constructor have no return type not even void. This is because the implicit return type of a constructor is the class type itself.

For example:

class Box
    {    double width;
          double height;
          double depth;

          Box()
          {    System.out.println("constructing box");
                width=10;
                height=10;
                depth=10;
         }

         double volume( )
         {      
                   return width*height*depth;
         }
     }
class BoxDemo
     {
               public static void main( String args[])
               {    Box mybox1=new Box( );
                     Box mybox2=new Box( );
                     double vol;
                     vol=mybox1.volume( );
                     System.out.println("Volume of first box is"+vol);
                     vol=mybox2.volume( );
                     System.out.println("Volume of second box is"+vol);
                 }
      }


the output of this program will be :   (How to compile and run a program)

Volume of first box is1000
Volume of second box is1000



* In order to pass different parameters to both boxes we have to use Parameterized constructor.