Thursday, 1 October 2015

FileWriter and FileReader in java with example


FileWriter

FileWriter creates a writer that you can use  to write a file.



Use constructors:


  1. FileWriter(String filepath)
       - It is create a new file and it get file name in string.
  1. FileWriter(String filepath, boolean path)
       - It is create a new file and it get file name in file object.


For Example


/**
 *
 * @author pradeep
 */

import java.io.*;  
class FileWriterDemo
{  
 public static void main(String args[])
{  
  try
   {  
   FileWriter fw=new FileWriter("abc.txt");  
   fw.write("Hello pradeep");  
   fw.close();  
  }
        catch(Exception e){System.out.println(e);
 }  
  System.out.println("success");  
 }  
}  



Result: Output of this program is

run:
success

BUILD SUCCESSFUL (total time: 0 seconds)





FileReader

It is a class which is responsible for class creates a reader that you can use read the contents of a file.

It does the following task:


  1. Connect to standard input console.
  2. Converts the bytes string coming from the source into char stream.
Use constructors:

  1. FileReader(String filepath)
  2. FileReader(File fileobj)

For Example


/**
 *
 * @author pradeep
 */


import java.io.*;  
class FileReaderDemo
{  
 public static void main(String args[])throws Exception
{  
  FileReader fr=new FileReader("abc.txt");  
  int i;  
  while((i=fr.read())!=-1)  
  System.out.println("FileReader is:"+(char)i);  
  
  fr.close();  
 }  
}  





Result: Output of this program:



run:
FileReader is:H
FileReader is:e
FileReader is:l
FileReader is:l
FileReader is:o
FileReader is: 
FileReader is:p
FileReader is:r
FileReader is:a
FileReader is:d
FileReader is:e
FileReader is:e
FileReader is:p
BUILD SUCCESSFUL (total time: 0 seconds)








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");

   }

}