Polymorphism


  • Defining multiple methods with same name
  • Static polymorphism and dynamic polymorphism

Static polymorphism:

  • Its a process of defining multiple methods with the same name and with different parameter types or list or order is called static polymorphism
  • Compile time itself we can say which method is going to get executed based on parameters types.
  • So its also called compile time polymorphism.
  • This will be achieved by method overloading.
  • Method overloading means defining multiple methods with same name and different parameters.
  • If two methods have same method name , But different parameter types those methods are considered as overloaded methods.
  • Will see example program on method overloading.

 Example program on method overloading:


  1. package instanceofjava;
  2.  
  3. public class OverloadingDemo {
  4.  
  5. void add(){
  6.     System.out.println("No-arg method");
  7. }
  8.  
  9. void add(int i){
  10.     System.out.println("int-arg method");
  11. }    
  12.  
  13. void add(String str){
  14.     System.out.println("String-arg method");
  15. }
  16.  
  17. void add(float d){
  18.     System.out.println("float-arg method");
  19. }
  20.  
  21. void add(int a, int b){
  22.     System.out.println("2 int-arg method");
  23. }
  24.  
  25. public static void main(String[] args){
  26.  
  27.     OverloadingDemo obj= new OverloadingDemo();
  28.  
  29.     obj.add();
  30.     obj.add(10);
  31.     obj.add(1.2f);
  32.     obj.add("java");
  33.     obj.add(1, 2);    
  34.  
  35. }
  36. }

Output


  1. No-arg method
  2. int-arg method
  3. float-arg method
  4. String-arg method
  5. 2 int-arg method

Method overloading with reference types:

  • We can also overload method with reference types, check below program.

  1. package instanceofjava;
  2. public class Test {
  3.  
  4. void show(){
  5.  
  6.         System.out.println("test class show() method");
  7. }
  8.  
  9. }

  1. package instanceofjava;
  2.  
  3. public class Example {
  4.  
  5. void method(String s){
  6.  
  7.        System.out.println("String-arg");
  8.  
  9. }
  10.  
  11. void method(Test obj){
  12.  
  13.     System.out.println("method(Test obj)-arg");    
  14.    obj.show();
  15.  
  16. }
  17.  
  18.  public static void main(String[] args){
  19.  
  20.     Example obj= new Example();
  21.     obj.method("polymorphism definition in oops");
  22.     obj.method(new Test());
  23.  
  24.     }
  25. }

Output:


  1. String-arg
  2. method(Test obj)-arg
  3. test class show() method

Dynamic polymorphism:

  • Redefining the super class method in sub class is called "method overriding" .
  • Method overriding is a language feature that allows a sub class to provide a specific implementation of a method that is already provided by one of its super class.
  • The implementation in the sub class overrides the implementation in the super class.
  • This is also called dynamic polymorphism / runtime polymorphism.

  1. package instanceofjava;
  2.  
  3. public class A{
  4.  
  5. void show(String s){
  6.  
  7.   System.out.println("super class method");
  8.  
  9. }
  10.  
  11. }

  1. package instanceofjava;
  2.  
  3. public class B extends A{
  4.  
  5. void show(String s){
  6.  
  7.   System.out.println("sub class method");
  8.  
  9. }
  10.  public static void main(String[] args){
  11. B obj =new B();
  12. obj.show();
  13. }
  14. }

Output:

  1. sub class method

We can not override static methods:

  • Actually static variables and static methods are class level. they are not instance level.
  • So if you try to override a static method will throw compile error
  1. package instanceofjava;
  2. public class Super {
  3.     int a,b;
  4.     static void add(){
  5.         System.out.println("super class add() method");
  6.     }
  7. }


  1. package instanceofjava;
  2. public class Sub extends Super {
  3.  
  4.  void add(){ // ERROR:This instance method cannot override the static method
  5.         System.out.println("this is sub class add() method");
  6. }
  7.  
  8.     public static void main(String [] args){

  9.         Sub obj= new Sub();
  10.  
  11.     } 
  12. }



  • Static methods are class level so we need to access by using class name.

  1. package instanceofjava;
  2. public class Sub extends Super {
  3.  
  4.  static void add(){// works fine
  5.         System.out.println("this is sub class add() method");
  6. }
  7.  
  8. public static void main(String [] args){

  9.    Super.add();
  10.    Sub.add();
  11.  
  12.  
  13.     } 
  14. }

Output:


  1. super class add() method
  2. sub class add() method

Method Overriding - Exceptions:

  •  When we overriding a method from super class to sub class. we need to follow some rules
  • Whenever sub class method throws checked exception then super class method must be declared throws with same exception or super class of that checked exception.
  1. package instanceofjava;
  2. public class Super {
  3.     int a,b;
  4.     void add(){
  5.         System.out.println("super class add() method");
  6.     }
  7. }


  1. package instanceofjava;
  2. public class Sub extends Super {
  3.  
  4.  void add() throws IOException{ // ERROR:     - Exception IOException is not compatible with
  5.                                                       //throws clause in  Super.add()
  6.         System.out.println("this is sub class add() method");
  7. }
  8.  
  9.     public static void main(String [] args){

  10.         Sub obj= new Sub();
  11.  
  12.     } 
  13. }



  1. package instanceofjava;
  2. public class Super {
  3.     int a,b;
  4.     void add()throws IOException{
  5.         System.out.println("super class add() method");
  6.     }
  7. }


  1. package instanceofjava;
  2. public class Sub extends Super {
  3.  
  4.  void add() throws IOException{// works fine
  5.         System.out.println("this is sub class add() method");
  6. }
  7.  
  8.     public static void main(String [] args){

  9.         Sub obj= new Sub();
  10.  
  11.     } 
  12. }

Inheritance Definition and Programs

  • Getting the properties from one class object to other class is known as inheritance.
  • In this inheritance will have parent class and child class so getting the properties of parent class object to child class object known as inheritance.
  • Inheritance means to take something that already made.
  • Inheritance can be implemented in java by using "extends" and "implements' keywords
  • In this we have two types.
  • Multilevel inheritance
  • Multiple inheritance

Multilevel inheritance:

  • Getting the properties from super class object to sub class object level wise with different priorities known as multiple inheritance.

Super class:

  1. package instanceofjava; 

  2. public class Super {
  3.  
  4.     int a,b;
  5.  
  6. void add(){
  7.  
  8.     System.out.println((a+b));
  9.  
  10. }
  11.  
  12. }

Sub class:

  1. package instanceofjava;
  2.  
  3. public class Sub extends Super {
  4.  
  5. void show(){
  6.  
  7.       System.out.println("this is sub class show() method");
  8.  
  9. }
  10.  
  11. public static void main(String [] args){
  12.  
  13.        Sub obj= new Sub();
  14.  
  15.         obj.a=1;
  16.         obj.b=2;
  17.  
  18.         obj.add();
  19.         obj.show();
  20.  
  21.     }
  22. }



Output:

  1. 3
  2. this is sub class show() method

Example program #2:

  1. package instanceofjava;
  2.  
  3. public class A {
  4.  
  5.     int a,b;
  6.  
  7. public void show(){
  8.  
  9.   System.out.println("A class show method");
  10.  
  11. }
  12.  
  13. }



  1. package instanceofjava;
  2.  
  3. public class B extends A{
  4.  
  5.  
  6. public void print(){
  7.  
  8.   System.out.println("B class print method");
  9.  
  10. }
  11.  
  12. }

  1. package instanceofjava;
  2.  
  3. public class C extends B{
  4.  
  5.  
  6. public void display(){
  7.  
  8.   System.out.println("C class display method");
  9.  
  10. }
  11. public static void main(String [] args){
  12.  
  13.  C obj= new C();
  14.  
  15.    obj.print();
  16.    obj.show();
  17.    obj.display();

  18. }
  19.  
  20. }

Output:

  1. B class print() method
  2. A class show method
  3. Class C display method

Multiple inheritance:

  • Multiple inheritance means getting the properties from one class object to multiple class object with different priority.
  • Multiple inheritance concept is not supported by java

  1. package instanceofjava;
  2.  
  3. public class A {

  4.  
  5. public void show(){
  6.  
  7.   System.out.println("A class show method");
  8.  
  9. }
  10.  
  11. }

  1. package instanceofjava;
  2.  
  3. public class b {
  4.  
  5. public void show(){
  6.  
  7.   System.out.println("B class show method");
  8.  
  9. }
  10.  
  11. }


  1. package instanceofjava;
  2.  
  3. public class C extends A,B {  // not supported by java
  4.  
  5.  
  6.  
  7. public void print(){
  8.  
  9.   System.out.println("C class print method");
  10.  
  11. }
  12.  
  13. }


You Might Like:

1. Java Interview Programs On Core Java

2. Java Interview Programs On Collections 

3. What is The Output Of Following Programs Java Interview:

4. Java Concept and Example Program

Encapsulation in java with example program

What is meant by encapsulation in java?
  • Binding the data with its related functionalities known as encapsulation
  • Here data means variables and functionalities means methods.
  • So keeping the variable and related methods in one place.
  • That place is "class". class is the base for encapsulation.
  • Lets see example program on encapsulation, how the variables and methods defined in a class



What is encapsulation in object oriented programming?
 
Basic java example program on data encapsulation:
  1. package com.instanceofjava;
  2.  
  3. public class EncapsulationDemo {
  4.  
  5.   String name;
  6.   int rno;
  7.   String address;
  8.  
  9. public String getName() {
  10.        return name;
  11.  }
  12.  
  13. public void setName(String name) {
  14.         this.name = name;

  15. public int getRno() {
  16.        return rno;
  17. }
  18.  
  19. public void setRno(int rno) {
  20.      this.rno = rno;
  21. }
  22.  
  23. public String getAddress() {
  24.         return address;
  25. }
  26.  
  27.  public void setAddress(String address) {
  28.     this.address = address;
  29. }
  30.  
  31. public void showInfo(){
  32.  
  33.       System.out.println("Name: "+getName());
  34.       System.out.println("R.No: "+getRno());
  35.       System.out.println("Name: "+getAddress());
  36.  
  37.     }
  38. }


Class:

  • The above example program shows how the variables and methods will be there in a class.
  • So class is the base for encapsulation
  • class is user defined data type in java means by using class we can structure our own programs.
  • Lest see a example program on class.

  1. package com.instanceofjava;
  2.  
  3. public class ClassDemo {
  4.  
  5.     //variables of a class
  6.    String empName;
  7.    int empId;
  8.    String Designation;
  9.  
  10.  //getter and setter methods for variables 
  11.  //by using these methods we can set the value to the variable and gat the value from the
  12.  //variables
  13.  
  14.  public String getEmpName() {
  15.         return empName;
  16. }
  17.  
  18.  public void setEmpName(String empName) {
  19.       this.empName = empName;
  20. }
  21.  
  22. public int getEmpId() {
  23.        return empId;
  24. }
  25.  
  26. public void setEmpId(int empId) {
  27.         this.empId = empId;
  28. }

  29. public String getDesignation() {
  30.         return Designation;
  31. }
  32.  
  33.  public void setDesignation(String designation) {
  34.         Designation = designation;
  35. }

  36. }

Object:

  • Instance of class is known as object.
  • Instance means dynamic memory allocation. So dynamic memory allocation to class is called object.
  • By using "new" keyword the object will be created dynamically.
  • Class is a template. By using object we will get memory for all variables so that we can use them.

  • We can create number of objects for one class based on our requirements.
  • Below an example program on class and object.


  1. package com.instanceofjava; 
  2.  
  3. public class User {
  4.  
  5.    String Name;
  6.     int Id;
  7.     String address;
  8.  
  9.  public String getName() {
  10.      return Name;
  11. }
  12.  
  13. public void setName(String name) {
  14.         Name = name;
  15. }
  16.  
  17. public int getId() {
  18.       return Id;
  19. }
  20.  
  21. public void setId(int id) {
  22.        Id = id;
  23. }
  24.  
  25. public String getAddress() {
  26.         return address;
  27. }
  28.  
  29. public void setAddress(String address) {
  30.         this.address = address;
  31. }
  32.  
  33. public static void main(String [] args){
  34.  
  35.        User obj= new User();
  36.  
  37.         obj.setName("sai");
  38.         obj.setId(1);
  39.         obj.setAddress("xyz");
  40.  
  41.         System.out.println(obj.getName());
  42.         System.out.println(obj.getId());
  43.         System.out.println(obj.getAddress());
  44.  
  45.     }
  46. }

Output:

  1. sai
  2. 1
  3. xyz


encapsulation in java

Program Check even or odd without using modulus and division operators

write a progrma to check number is even or odd without using modulus or divition

  • Checking the number even or odd program is very easy. Anybody can solve this but there is a condition we need to see.
  • When we get this question in interview "write a program to check given number is even or odd" always continues with "without using modulus and division operators".
  • Before going to actual program lets see how to check a number is even or odd by using modulus and division operators.

 

 

Program to check number is even or odd by using modulus "%" operator

  1. package instanceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class EvenorOdd {
  5.  
  6. public static void main(String []args )    {
  7.  
  8.     int number;
  9.     Scanner in= new Scanner(System.in);
  10.  
  11.     System.out.println("Enter a number to check even or odd");
  12.     number=in.nextInt();
  13.  
  14.     if((number % 2)==0){
  15.         System.out.println(+number+" is Even number");
  16.     }else{
  17.         System.out.println(+number+" is Odd Number");
  18.     }
  19.  
  20. }
  21. }

Output:

  1. Enter a number to check even or odd
  2. 37
  3. 37 is Odd Number


Program to check number is even or odd by using division "/" operator

  1. package instanceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class EvenorOdd {
  5.  
  6. public static void main(String []args )    {
  7.  
  8.     int number;
  9.     Scanner in= new Scanner(System.in);
  10.  
  11.     System.out.println("Enter a number to check even or odd");
  12.     number=in.nextInt();
  13.  
  14.     if((number / 2)*2==number){
  15.         System.out.println(+number+" is Even number");
  16.     }else{
  17.         System.out.println(+number+" is Odd Number");
  18.     }
  19.  
  20. }
  21. }

Output:

  1. Enter a number to check even or odd
  2. 46
  3. 46 is Even Number


Without using modulus and division operators:

  • The above two programs will check number is even or odd and displays result. 
  • Now we need to write a program to check even or odd without using modulus and division operators.
  • It is very simple if you know about operators including "BIT WISE".
  • Yes using Bit Wise AND "&" operator we can check a number is even or odd.
  • Before starting our program lets see how this bit wise AND "&" operator will work.

Bitwise Operators :

  • Bit wise operators will work on bits at a time.
  • AND : 1 & 1=1
  • OR :     0 | 1= 1 , 1 | 0=1 , 1| 1= 1
  • XOR:   0 ^ 1= 1 , 1^ 0=1
  • NOT : !0=1
  • Take two number 2 and 3
  • 010 : 2
    011 : 3
    ------
    010 : 2
  • ------
  • Take two numbers 2 and 1
  • 010  :2
    001  :1
    -----
    000  :0
    -----
  • From above example we can say that on every even number & 1 gives 0.
  • So this is our logic to be implemented in our program  if "Number & 1==0" then its even number.

Program to check number is even or odd by using  "&" operator

  1. package instanceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class EvenorOdd {
  5.  
  6. public static void main(String []args )    {
  7.  
  8.     int number;
  9.     Scanner in= new Scanner(System.in);
  10.  
  11.     System.out.println("Enter a number to check even or odd");
  12.     number=in.nextInt();
  13.  
  14.     if((number & 1)==0){
  15.         System.out.println(+number+" is Even number");
  16.     }else{
  17.         System.out.println(+number+" is Odd Number");
  18.     }
  19.  
  20. }
  21. }


Output:

  1. Enter a number to check even or odd
  2. 9
  3. 9 is Odd Number


Using Shift Operators:

  • We can check even or odd by using shift operators also may be it is not a better solution but  trying to cover alternative.
  • Lets see how shift operators will work
  • Lets do this 2 >> 1
  • 2 means 010 so now we need to shift right side by 1 place
  • 010
    -----
    001
  • Here we added one "0" to the left side to shift right 
  • So the value became 1
  • Now will try left shift  001 >> 1
    001
    ----
    010
  • 010<<1=001(1) and 001 >> 001=010(2)
  • By this we can say if a (number >>1)<<1 gives same then that is even number. result=input
  • Lets check for odd number 3
  • 011>>1=001 which is 1 and 001<<1=010 which is 2 so (3>>1)>>1=2 so result!=input

Program to check number is even or odd by using Shift operator

  1. package instanceofjava;
  2. import java.util.Scanner;
  3.  
  4. public class EvenorOdd {
  5.  
  6. public static void main(String []args )    {
  7.  
  8.     int number;
  9.     Scanner in= new Scanner(System.in);
  10.  
  11.     System.out.println("Enter a number to check even or odd");
  12.     number=in.nextInt();
  13.  
  14.     if( ( number >> 1) <<1==number){
  15.         System.out.println(+number+" is Even number");
  16.     }else{
  17.         System.out.println(+number+" is Odd Number");
  18.     }
  19.  
  20. }
  21. }

Output:

  1. Enter a number to check even or odd
  2. 64
  3. 64 is Even Number
Here is the another solution for this.

  1. package instanceofjava;
  2. import java.util.Scanner;
  3.  
  4. class EvenOrOddDemo
  5. {
  6.  
  7. public static void main(String[] args)
  8. {
  9.  
  10. Scanner sc=new Scanner(System.in);
  11.  
  12. System.out.println("Enter a number to check whether it is even or odd without using Modulus
  13. and Division: ");
  14.  
  15. int n=Integer.parseInt(sc.next());
  16. float f=(float)n/(float)2;
  17. int d=(int)f*2;
  18.  
  19. if(d==n)
  20. System.out.println(n+" is a even number");
  21. else
  22. System.out.println(n+" is a odd number");
  23. }

  24. }

Output:

  1. Enter a number to check whether it is even or odd without using Modulus and Division
  2. 4
  3. 4 is Even Number



You might like:

ObjectInputStream and ObjectOutputStream

  • ObjectInputStream and ObjectOutputStream classes are used to store objects state permanently in files or to send to remote computer via network.
  • ObjectOutputStream class is subclass of ObjectOutput interface. It implements below method to write object to underline output stream.

  1. public void writeObject(Object ob)throws IOException

  • ObjectInputStream class is a subclass of ObjectInput interface. It implements below method to read object to underline output stream.
  1. public Object readObject() throws IOException

Rule:

  • To write or send object to external world it must be java.io.Serializable interface type.
  • It means this objects class must be a sub class of java.io.Serializable interface, else write() method throws java.io.NotSerializableException.

Constructors to create above two classes objects:

  • Like DataInputStream and DataOutputStream , ObjectInputStream and  ObjectOutputStream are also can not connect to source and destination directly. So their constructors take other input and output stream class objects.
  1. public ObjectInputStream(InputStream in)
  2. public ObjectOutputStream(OutputStream out)

  1. package instanceofjava;
  2.  
  3. import java.io.FileOutputStream;
  4. import java.io.ObjectOutputStream;
  5. import java.io.Serializable;
  6.  
  7. public class SerDemo implements Serializable {
  8.  
  9.     String name;
  10.     String phonum;
  11.     String address;
  12.     int pin;
  13.  
  14.    public String getName() {
  15.        return name;
  16.     }
  17.  
  18.     public void setName(String name) {
  19.         this.name = name;
  20.     }
  21.  
  22.     public String getPhonum() {
  23.         return phonum;
  24.    }
  25.  
  26.     public void setPhonum(String phonum) {
  27.         this.phonum = phonum;
  28.     }
  29.  
  30.    public String getAddress() {
  31.         return address;
  32.     }
  33.  
  34.     public void setAddress(String address) {
  35.         this.address = address;
  36.     }
  37.  
  38.    public int getPin() {
  39.         return pin;
  40.     }

  41.   public void setPin(int pin) {
  42.         this.pin = pin;
  43.     }
  44.  
  45. public static void main(String[] args)throws Exception{
  46.  
  47.     SerDemo empobj= new SerDemo();
  48.     empobj.setName("sai");
  49.     empobj.setAddress("xxx, xxxstreet ");
  50.     empobj.setPhonum("040-9999999");
  51.     empobj.setPin(500072);
  52.  
  53.     ObjectOutputStream oos= new ObjectOutputStream(new
  54.     FileOutputStream("E:\\employee.ser"));
  55.  
  56.     oos.writeObject(empobj);
  57.  
  58. }
  59. }




  1. package instanceofjava;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.ObjectInputStream;
  5.  
  6. public class DeserDemo {
  7.  
  8. public static void main(String[] args)throws Exception{   
  9.  
  10.    ObjectInputStream ois= new ObjectInputStream(new FileInputStream("E:\\employee.ser"));
  11.       SerDemo obj= (SerDemo)ois.readObject();
  12.       System.out.println(obj.getAddress()):
  13.  
  14.     }
  15. }

Output:

  1. sai
  2. xxx, xxxstreet 
  3. 500072
  4. 040-9999999

  • This is the actual functionality of ObjectOutputStream and ObjectInputStream: Serialization and deserialization

Serialization:

  • Serialization is the process of converting objects into stream of  bytes and sending them to underlying output stream.
  • Using serialization we can store object state permanently in a destination , for example on remote computer.
  • Serialization operation performed by the writeObject() method of ObjectOutputStream.

Deserialization:

  • Deserialization is the process of converting stream of bytes into original object.
  • Deserialization operation is performed by readObject() of ObjectInputStream.

Rule:

  • Only java.io.serializable type objects are serialized. Serializable is a marker interface, it does not have any methods.  
  • It provides special permission or identity to JVM to serialize Object.
  • If the given object is not of type serializable interface then writeObject() will throw an unchecked exception called java.io.NotSerializableException.

DataInputStream and DataOutPutStream

  • These classes are used to read and write data as primitive type from the underlying InputStreams and Output Streams.
  • DataInputStream and DataOutputStream classes have special methods to perform reading and writing operations as primitive data.

DataInputStream:

  • DataInputStream is a sub class of FilterInputStream and DataInput Interface.
  • It implements  below methods from the DataInput interface for reading bytes from a binary stream and convert them in to corresponding java primitive type.

 DataInputStream methods:

  • public byte readByte()
  • public short readShort()
  • public int readInt()
  • public long readLong()
  • public float readFloat()
  • public double readDouble()
  • public char readChar()
  • public boolean readBoolean()
  • public String readUTF()
  • public String readLine()

DataOutPutStream:

  • DataOutputStream class is a subclass of FilterOutputStream and DataOutput interface.
  • It implements below methods from DataOutput interface for converting data of any java primitive types to a stream of bytes and writing these bytes to a binary stream.


DataOutPutStream Methods:

  • public void writeByte(byte b)
  • public void writeShort(short s)
  • public void writeInt(int i)
  • public void writeLong(long l)
  • public void writeFloat(float f)
  • public void writeDouble(double d)
  • public void writeChar(char ch)
  • public void writeBoolean(boolean b)
  • public void writeUTF(String s)
  • public void writeBytes(String s)

  • To read data as primitive types using above readXxx() methods , the data must be written to the file as primitive types using writeXxx() methods.
  • readXxx() methods must be called in the same order in which the writeXxx() methods are called otherwise wrong value will be returned or EOFException will occur.

Program to Write data as primitive type using DataInputStream


  1. package instanceofjava;
  2.  
  3. import java.io.DataOutputStream;
  4. import java.io.FileOutputStream;
  5.  
  6. public class DOSDemo {
  7.  
  8. public static void main(String [] args) throws Exception{
  9.  
  10.   FileOutputStream fos= new FileOutputStream("data.txt");
  11.   DataOutputStream dos = new DataOutputStream(fos);
  12.  
  13.     dos.writeInt(37);
  14.     dos.writeFloat(2.15f);
  15.     dos.writeChar('c');
  16.     dos.writeBoolean(true);
  17.     dos.writeUTF("instance of java");
  18.  
  19.   }
  20. }



  • After compilation and execution in current working directory you can find the "data.txt".
  • Check data.txt file size.
  • To check file size "right click on the file"-> click on properties
  • You will get size = size of all primitive data types mentioned in program check it.
  • From this program we can prove boolean takes 1 byte, the minimum memory location size in java is 1 byte. 
  • For string data including double quotes JVM provides 1 byte for each character.

Program to Read data as primitive type using DataOutputStream

  1. package instanceofjava;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.FileInputStream;
  5.  
  6. public class DISDemo {
  7.  
  8. public static void main(String [] args) throws Exception{
  9.  
  10.     FileInputStream fis= new FileInputStream("D:\\data.txt");
  11.    DataInputStream dis = new DataInputStream(fis);
  12.  
  13.     int i=    dis.readInt();
  14.    float f=dis.readFloat();
  15.    char ch=dis.readChar();
  16.    boolean b=dis.readBoolean();
  17.    String str= dis.readUTF();
  18.  
  19.     System.out.println(i);
  20.     System.out.println(f);
  21.     System.out.println(ch);
  22.     System.out.println(b);
  23.     System.out.println(str);
  24.  
  25.     }
  26. }

  • Execute the above application by changing readXxx() methods calling order.
  • Then check you will get right value or not.
  • Actually readXxx() method read number of bytes from the file based on the data type.
  • For example if we call readInt() method it read 4 bytes from file.
  • So if our bytes are available in file program executed fine, execution terminates with EOFException.

Limitations in DataInputStream and DataOutputStream:

  • Using DataInputStream and DataOutputstream we can not read and write objects from persistence media.
  • They have only capability to read data up to only primitive data types.

Solution:

  • To read and write objects we must use ObjectInputStream and ObjectOutputStream.
Select Menu