Wednesday 30 September 2015

Inheritance in java with example

It is a mechanism in which one object acquires  all the properties and behaviors of parent object. 

It is a building block. It is the property by which an object acquires properties and behaviors of another object. 

Example: Parent-child relationship here child is inherit of parent properties and behaviors.


Types of Inheritance: There are three types of inheritance shown in figure below.





1. Single Inheritance : In this we can have single base class and n number if drive class.


## Example

class a
 {
    int a;
  void display()
   {
      System.out.println("Display method");
    }
 class b extends a
  {
  }
}

2. Multi Level inheritance 

It is just concept of grand parent class child class inheritance of parent class and parent-class is inheritance of grand-parent-class so in this case grand parent-class is implicitly inheriting the properties and the method of grand-parent along with parent-class that's is called multi level inheritance.

## For example 1.

class A
  {
     System.out.println("Grand-parent-class");
   }
class B extends A
 {
      System.out.println("Parent-class inheritance of grand-parent");
 }
class C extends B
 {
      System.out.println("child-class inheritance of parent-class");
 }


## Example 2.


package com;




/**

 *

 * @author pradeep

 */



class bike{

public bike()

{

System.out.println("Class bike");

}

public void vehicleType()

{

System.out.println("Vehicle Type: bike");

}

}

class hero extends bike{

public hero()

{

System.out.println("Class hero");

}

public void brand()

{

System.out.println("Brand: hero");

}

public void speed()

{

System.out.println("Max: 70Kmph");

}
}
public class honda extends hero{

 public honda()
 {
 System.out.println("honda : 80kmph");
 }
 public void speed()
{
System.out.println("Max: 120Kmph");
}
 public static void main(String args[])
 {
 honda obj=new honda();
 obj.vehicleType();
 obj.brand();
 obj.speed();
 }
}

  
Output : Output of this program is below.
run:

Class bike

Class hero

honda : 80kmph

Vehicle Type: bike

Brand: hero

Max: 120Kmph

BUILD SUCCESSFUL (total time: 1 second)

3. Multiple Inheritance :

In this we can have n numbers of parents and a single class which drives from it.

## For Example 1.

 class A
   {
       void display()
         {
         
         }
       class B
            {
                   void display()
                          {
           
                           }
                   class C extends A,B
                       {
                             C C1=new C1();
                             C1.disp();
                          }
            }
   }

## Example 2.



/**

 *

 * @author pradeep

 */

public class MultipleInheritance 
{

    

}

interface A

{

   public void MultipleInheritance();

}

interface B

{

   public void MultipleInheritance();

}

class MeltiDemo implements A, B

{

   public void MultipleInheritance()

   {

       System.out.println(" Multiple inheritance");

   }

}

Monday 28 September 2015

How to set path in java

This is important for us how to set path in java you have set path in ways :
  1. Temporary way 
  2. Permanent way
Now we discus about 


1. How to set path in Temporary way of JDK in my windows.

You have just flow my flowing steps:
  1. Open command prompt or CMD.
  2. Copy the path of jdk or bin directory.
  3. Now write command prompt:- set path=copied of jdk or bin path.

## For example

set path=C:\Program Files\Java\jdk1.8.0_05\bin





2. How to set path in Permanent way of JDK in my windows.

You have just flow my flowing steps:
  • Go to MyComputer properties -> advanced tab -> environment variables -> new tab of user variable -> write path in variable name -> write path of bin folder in variable value -> ok -> ok -> ok

where is use of java?

Java is high label, robust, secured, platform independent and object- oriented  programming language.




## Which is used to following  devices


  1. Desktop application like as media player, antivirus, notepad etc.
  2. Web application like as any website eg: amazon, snapdeal etc.
  3. Enterprise applications like as any types of app ex: banking applications.
  4. Embedded system
  5. Mobile
  6. Smart card
  7. Robotics
  8. Games and many more

StringBuffer in java with examples

Java stringBuffer class is used to created mutable string. The string java is same as string class except it is mutable that us can be changed.

String buffers are preferred when heavy modification of character strings is involved (appending, inserting, deleting, modifying etc).


## StringBuffer Functions : The following program explains the usage of the some of the basic StringBuffer methods like ;





  1. capacity() Returns the current capacity of the String buffer.
  2.  length() Returns the length (character count) of this string buffer.
  3. charAt(int index) The specified character of the sequence currently represented by the string buffer, as indicated by the index argument, is returned.
  4. setCharAt(int index, char ch) The character at the specified index of this string buffer is set to ch.
  5. toString() Converts to a string representing the data in this string buffer.
  6.  insert(int offset, char c) Inserts the string representation of the char argument into this string buffer.
  7. delete(int start, int end) Removes the characters in a substring of this StringBuffer
  8. replace(int start, int end, String str) Replaces the characters in a substring of this StringBuffer with characters in the specified String.
  9. reverse() The character sequence contained in this string buffer is replaced by the reverse of the sequence.
  10. append(String str) Appends the string to this string buffer.
  11. setLength(int newLength).




Note 


  1. That the StringBuffer class has got many overloaded ‘append’ methods which can be used based on the application need.
  2. That the StringBuffer class has got many overloaded ‘insert’ methods which can be used based on the application need.





# For Example:





/**
 *
 * @author pradeep
 */

public class DemoStringBuffer 
{

public static void main(String[] args)
 {
//     Examples of Creation of StringsBuffer
StringBuffer strBuff1 = new StringBuffer("Bob");
StringBuffer strBuff2 = new StringBuffer(400); //With capacity 100
StringBuffer strBuff3 = new StringBuffer(); //Default Capacity 16
System.out.println("stringBuffer1 : " + strBuff1);
System.out.println("stringBuffer2 capacity : " + strBuff2.capacity());
System.out.println("stringBuffer3 capacity : " + strBuff3.capacity());
}
}







Result:  Output of this program is:





run:


stringBuffer1 : Bob


stringBuffer2 capacity : 400


stringBuffer3 capacity : 16

BUILD SUCCESSFUL (total time: 1 second)





Note: If you have any problem then reply me.



Friday 25 September 2015

What is an Array in java with example

Array is collection of similar kind of data. Array in java is index based , first element of the array is stored at 0 index.






## Benefits (Advantages) of array

  1.  Code Optimization
  2.  Performance increase




Disadvantage Disadvantage of array is "size limit ".




Types of array:

  1. Single Dimensional Array 
  2. Multidimensional Array




Many types of syntax to declared in array:

  1. dataType[] arr; (or)  
  2. dataType []arr; (or)  
  3. dataType arr[];  


1 Example of single Dimensional array


/**

 *

 * @author pradeep

 */

class ArrayDemo {

    public static void main(String[] args) {

        /*int marks[]=new int[5];

        marks[0]=33;

        marks[1]=65;

        marks[2]=69;

        marks[3]=78;

        marks[4]=86;

        * */

        //int marks[]=new int[]{33,65,69,78,86};

        int marks[]={33,65,69,78,86};

        for(int i=0;i<5;i++)

        {

            System.out.println("print marks:"+marks[i]);

        }

    }

    

}




2. Example of MultiDimensional array

/**
 *
 * @author pradeep
 */


class MultiDimensionalArray{  
public static void main(String args[]){  
  
//declaring and initializing 2D array  
int array[][]={{6,7,8},{4,6,8},{3,6,9}};  
  
//printing 2D array  
for(int i=0;i<3;i++){  
 for(int j=0;j<3;j++){  
   System.out.print("Multi Dimensional Array" +array[i][j]+" ");  
 }  
 System.out.println();  
}  
  
}}  

Result: Output of this result is:


run:

Multi Dimensional Array6 Multi Dimensional Array7 Multi Dimensional Array8 

Multi Dimensional Array4 Multi Dimensional Array6 Multi Dimensional Array8 

Multi Dimensional Array3 Multi Dimensional Array6 Multi Dimensional Array9 

BUILD SUCCESSFUL (total time: 0 seconds)







Note: If you have any problem then reply me.

Method overriding or Function overriding with example

Function overriding is just easy to say if sub class or child class has the same method or function declared in the parent class that types of method is called function or method overriding.

Declaring a method in subclass (child class) which function is already  declared in  parent class is known as method overriding.





## For Example:


/**
 *
 * @author pradeep
 */
class HumanBeing
{
   public void social()
   {
      System.out.println("Human is eating");
   }
}
class Boy extends HumanBeing{
   public void social(){
      System.out.println("Boy is eating");
   }
   public static void main( String args[]) 
{
      Boy obj = new Boy();
      obj.social();
   }
}




Result: Result of this program is

run:
Boy is eating
BUILD SUCCESSFUL (total time: 0 seconds)

Note: If you have any problem then reply me.