Monday 19 October 2015

What is Static binding and Dynamic binding in java with example

There are two types of Binding shown in figure:







Static binding



Static binding is also known as Early binding. It can be resolved at compile time by compiler is know as static binding.

It all static, private and final method have also be bonded at compile time.




For Example

Create a class which name is StaticBinding.java


package com;

/**
 *
 * @author pradeep
 */

class StaticBinding{

}
class Binding extends StaticBinding{
   public void RunBinding(){
      System.out.println("Compile Static Binding");
   }
   public static void main( String args[]) {
      Binding obj1 = new Binding();
      obj1.RunBinding();
   }
}



Result: Output of this program is

run:
Compile Static Binding
BUILD SUCCESSFUL (total time: 1 second)





Dynamic Binding :

Dynamic binding is also known as Late binding. It is resolved the problems at run time after static binding.

It is overriding both class as parent class and child class same methods. It is use for overridden method.


For Example:



package in;

/**
 *
 * @author pradeep
 */
class StaticBinding{
   public void RunBinding()
   {
       System.out.println("Compile Dynamic Binding");
   }
}
class Binding extends StaticBinding{
   public void RunBinding(){
       System.out.println("Compile Static Binding");
   }
   public static void main( String args[]) {
       
       StaticBinding obj2 = new Binding();

       obj2.RunBinding();
   }
}



Result: Output of this program is

run:
Compile Static Binding
BUILD SUCCESSFUL (total time: 2 seconds)


Saturday 17 October 2015

What is Aggregation in java with example

Aggregation is a relationship between two class like association. It is represents as Has-A relationship.


For example we take two class which name is company class and employee class.



For Example



Create a class which name is AggregationCompanyClass.java



/**

 *
 * @author pradeep
 */

class AggregationCompanyClass

{
   int JobId;
   String Name;
   String MobNumber;
   AggregationCompanyClass(int jobId, String name, String mobnumber)
   {
       this.JobId=jobId;
       this.Name =name;
       this.MobNumber = mobnumber;
     
   }
}
class EmployeeClass
{
   int EmpId;
   String EmpName;
   AggregationCompanyClass EmpAddr; 
   EmployeeClass(int id, String name, AggregationCompanyClass addr){
       this.EmpId=id;
       this.EmpName=name;
       this.EmpAddr = addr;
   }
   public static void main(String args[]){
       AggregationCompanyClass addrs = new AggregationCompanyClass(9897357, "HCL", "8000909997");
       EmployeeClass empObj = new EmployeeClass(371, "Monty", addrs);
       System.out.println(empObj.EmpId);
       System.out.println(empObj.EmpName);
       System.out.println(empObj.EmpAddr.JobId);
       System.out.println(empObj.EmpAddr.Name);
       System.out.println(empObj.EmpAddr.MobNumber);
   }

}


Result : Output of this program is



run:

371
Monty
9897357
HCL
8000909997

BUILD SUCCESSFUL


Note : Aggregation is use to maintain code re-usability.  


Tuesday 13 October 2015

Encapsulation in java with example

Encapsulation is the whole idea behind encapsulation is to hide the implementation details from the users. 

It just like capsule, Its wrapping code and data together into a single unit.






According to figure encapsulation class is just making all the data members of the class private.

It is also known as data hiding.





# Methods of Encapsulation

  1. setter( )
  2. getter( )

1. setter( ) - setter is used for set the data and 
2. getter( )getter is use for get the data.


## For Example:


**Create a java class Name  is EncapsulationExample.java

/**
 *
 * @author pradeep
 */

public class EncapsulationExample
{
    private int spmt;
    private String NameOfEmp;
    private int AgeOfEmp;

    //Use of Getter and Setter methods
    public int getEmpSPMT()
    {
        return spmt;
    }

    public String getEmpName()
    {
        return NameOfEmp;
    }

    public int getEmpAge()
    {
        return AgeOfEmp;
    }

    public void setEmpAge(int newValue)
    {
        AgeOfEmp = newValue;
    }

    public void setEmpName(String newValue)
    {
        NameOfEmp = newValue;
    }

    public void setEmpSPMT(int newValue)
    {
        spmt = newValue;
    }
}

~~~~~~~~~~~~~~~~~~~~~~~~~~*****^^^^^^^^*****~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**Create a java class Name of java class is TestEnsaptulation.java

/**
 *
 * @author pradeep
 */
public class TestEnsaptulation
{
    public static void main(String args[])
{
         EncapsulationExample obj = new EncapsulationExample();
         obj.setEmpName("monty");
         obj.setEmpAge(23);
         obj.setEmpSPMT(112233);
  System.out.println("Name Of Employee: " + obj.getEmpName());
 System.out.println("SPMT Of Employee: " + obj.getEmpSPMT());
 System.out.println("Age Of Employee: " + obj.getEmpAge());
    } 
}


Output : Output of this program:


run:

Name Of Employee: monty
SPMT Of Employee: 112233
Age Of Employee: 23

BUILD SUCCESSFUL (total time: 2 seconds)

Saturday 10 October 2015

what is Access modifier in java with example

Access is members with respective to other class, data and member. It is the way of specifying the accessibility.




Types of Access Modifier:


There are four basic Access modifier in java
  1. public
  2. private
  3. protected and
  4. default

1. public : It is define if top level of interface within a package is declared as public. Then it means it is accessible inside and outside of the package. It is accessible anywhere class is visible by public.




For Example

Create a java class which name of class is A_Class.java



package com;



/**

 *

 * @author pradeep

 */

public class A_Class {

  

public void msssageShow()

{

    System.out.println("Hello I am A_Class");

}  






Create a java class which name of class is B_Class.java





package puter;

import com.*;

/**

 *

 * @author pradeep

 */

    

class B_Class{  

  public static void main(String args[])

  {  

   A_Class obj = new  A_Class();  

   obj.msssageShow();  

  }  
}   



Result : Output of this program is


run:

Hello I am A_Class

BUILD SUCCESSFUL (total time: 1 second)



2. Private : This class is visible only one like as admin. They are accessible if and only if its is same class.



For Example

Create a java class which name of class is A_private_Class.java


/**

 *

 * @author pradeep

 */



class A_private_Class

{  

private int data=10;  

private void massage()

{

    System.out.println("Hello I am A_private_Class ");

}  

}  





Create a java class which name of class is B_Show_Class.java



/**
 *
 * @author pradeep
 */

public class B_Show_Class{  
 public static void main(String args[]){  
   A_private_Class obj=new A_private_Class();  
   System.out.println(obj.data);
//Show Compile Time Error  
   obj.massage();
//Show Compile Time Error  
   }  

ResultCompile time error show on your windows.



3. protected : This class is visible package and outside of package. If members are declared  as protected then these are accessible to all the classes in the package and its to all subclasses package is visible.


Example

Create a java class which name of class is A_Protected_Class.java
 package com;

/**
 *
 * @author pradeep
 */
public class A_Protected_Class 
protected void massage()
{
    System.out.println("Hello I am A_Protected_Class");
}  
}  

~~~~~~~~~~~~~~~~******^^^^^^^^******~~~~~~~~~~~~~~~~

**
Create a java class which name of class is B_Protected_Class.java

package puter;
import com.*;
/**
 *
 * @author pradeep
 */

class B_Protected_Class extends A_Protected_Class
{  
  public static void main(String args[])
  {  
   B_Protected_Class obj = new B_Protected_Class();  
   obj.massage();  
  }  
}  

  
Result : Output of this program is

run:
Hello I am A_Protected_Class
BUILD SUCCESSFUL (total time: 4 seconds) 


4. default : It is accessible only to the other classes in the class's or package.

For Example:


**
Create a java class which name of class is A_Default_Class.java


package com;



/**

 *

 * @author pradeep

 */


class A_Default_Class{  

  void massage()

  {

      System.out.println("Hello I am A_Default_Class");

  }  

}  



**

Create a java class which name of class is B_Default_Class.java


package puter;
import com.*; 
/**
 *
 * @author pradeep
 */
class B_Default_Class
{  
  public static void main(String args[])
  {  
   A_Default_Class obj = new A_Default_Class();
//Show Compile Time Error  
   obj.massage();
//Show Compile Time Error  
  }  
}  




Result : Output of this program is


Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - com.A_Default_Class is not public in com; cannot be accessed from outside package

at puter.B_Default_Class.main(B_Default_Class.java:17)

Java Result: 1

BUILD SUCCESSFUL (total time: 2 seconds)




Thursday 8 October 2015

Multithreading in java with example

Multithreading is to provide simultaneous execution of two or more parts of a program to maximum  utilize in the CPU time. In this threading program contains two or more parts that can be run concurrently.


Threads are lightweight processes they share the same address space. multiprocessing and multithreading both are used to achieved multitasking. It is mostly used in games, animations.



Threads can be created into two ways:

  1. By extending thread class
  2. By implementing runnable interface










Methods of Threads:

  1. getName( ) - It is used to be obtaining a threads name.
  2. getPriority( ) - It is used to be obtaining a thread's priority.
  3. getAlive( ) - It is used to be obtaining determine if a thread is still running.
  4. start( ) -  It is used to be obtaining start a thread by calling it run() method.
  5. sleep( ) - It is used to be suspend a thread for a period of time.
  6. run( ) - It is used to be entry point of thread.
  7. join( ) - It is used to be wait for a thread to terminate.


For Example

Create a class name is "SimpleThread.java"

/**
 *
 * @author pradeep
 */

class SimpleThread implements Runnable
{  
  public void run()
{  
    System.out.println("Hellow I am Thread Now I am running.");  
  }   
  public static void main(String args[])
{  
     SimpleThread obj=new SimpleThread();  
     Thread th =new Thread(obj);  
     th.start();  
 }  

}


Result: Output of this program is:

run:
Hellow I am Thread Now I am running.

BUILD SUCCESSFUL (total time: 0 seconds)



Another Example Of Thread:


/**
 *
 * @author pradeep
 */
class CountThread implements Runnable
{
   Thread demothread ;
   CountThread()
   { 
      demothread = new Thread(this, "demo runnable thread");
      System.out.println("demo thread created" + demothread);
      demothread.start();
   }
   public void run()
   {
      try
      {
        for (int t=0 ;t<14;t++)
        {
          System.out.println("Printing the CountThread Now! " + t);
          Thread.sleep(1000);
        }
     }
     catch(InterruptedException ex)
     {
        System.out.println("demo thread interrupted Now!");
     }
     System.out.println("demothread run is over Now!" );
   }
}
class RunnableDemoThread
{
    public static void main(String args[])
    {
       CountThread cnt = new CountThread();
       try
       {
          while(cnt.demothread.isAlive())
          {
            System.out.println("Parent thread will be Alive till the Chlid thread is live"); 
            Thread.sleep(2000);
          }
       }
       catch(InterruptedException ex)
       {
          System.out.println("Parent thread interrupted Now!");
       }
       System.out.println("Parent thread run is over Now!" );
    }

}





Result:
 Output of this program is:

run:
demo thread createdThread[demo runnable thread,5,main]
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 0
Printing the CountThread Now! 1
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 2
Printing the CountThread Now! 3
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 4
Printing the CountThread Now! 5
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 6
Printing the CountThread Now! 7
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 8
Printing the CountThread Now! 9
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 10
Printing the CountThread Now! 11
Parent thread will be Alive till the Chlid thread is live
Printing the CountThread Now! 12
Printing the CountThread Now! 13
Parent thread will be Alive till the Chlid thread is live
demothread run is over Now!
Parent thread run is over Now!

BUILD SUCCESSFUL (total time: 17 seconds)





Advantage Of Thread:



  1. You can perform many operations  together so that's why your time not waste.
  2. Threads are independent so it is does't any  affect other thread if exception occur.

Disadvantage Of Thread:

  1. Difficulty of writing codes
  2. Difficulty of debugging
  3. Difficulty of testing.





Tuesday 6 October 2015

What is Interface in java with example

Interface is a mechanism to achieve fully abstraction. It's look looks like as class but not a class. It solve is with a powerful construct called interface.



It can be used to fully abstraction, generic template and multiple inheritance in java. It a blueprint of a class.











For Example:




/**
 *
 * @author pradeep
 */

interface interfaceprint
{  
void printinterface();  
}  
  
class intr implements interfaceprint
{  
public void printinterface()
{
    System.out.println("Hello i am interface");
}  
  
public static void main(String args[])
{  
intr obj = new intr();  
obj.printinterface();  
 }  



Result: Output of this program is 

run:
Hello i am interface

BUILD SUCCESSFUL (total time: 1 second)








Multiple Inheritance in java

Multiple inheritance in java if a class implements multiple interface or extends multiple interface that's is called multiple interface.


For Example:


/**
 *
 * @author pradeep
 */
interface Multiinterfaceprint
{  
void printmultiinterface();  
}  
  
interface Multiinterfaceshow
{  
void showmultiinterface();  
}  
  
class multiintr implements Multiinterfaceprint,Multiinterfaceshow
{  
  
public void printmultiinterface(){System.out.println("Hello i m interface");
}  
public void showmultiinterface()
{
System.out.println("Welcome to my city");
}  
  
public static void main(String args[])
{  
multiintr obj = new multiintr();  
               obj.printmultiinterface();  
               obj.showmultiinterface();  
 }  




Result: Output  of this program is  


run:
Hello i m interface
Welcome to my city

BUILD SUCCESSFUL (total time: 0 seconds)




Advantage of interface:


  1. Without bothering about implementation; we can achieve the security of implementation.
  2. Multiple interface can't be allow, however by using interface you can achieve the same.
  3. Interface are mainly used to provide polymorphic behavior.


Disadvantage of interface:

  1. Interface are slower and more limited than other ones.
  2. Interface should be used multiple number of times otherwise it hardly any use of having them.