Topics

6/recent/ticker-posts

Abstraction in java

 Abstraction:

  • Hiding behavior implementation from behavior. 
  • Hiding method implementation from method declaration is called as Abstraction. 
  • The benefit of abstraction is to achieve Run Time Polymorphism (RTP). 
  • If abstraction doesn't exist and in Emp.java in retrieveEmp() method if the emp details are retrieved from file and when such a class is used by the client, client will always get the emp data from file system but not from excel/DB/cloud.


  • As and when the provider want Emp class to fetch data from excel/DB then provider must write one more version of Emp class and such a class should be provided to client again.


  • Client may often need to change Emp class instantiation wherever Emp class is used through out the application which is practically impossible.


  • Provider instead of providing a Emp class with retrieveEmp() method implementation, if retrieveEmp() method is declared as abstract in Emp class, the class also must be declared as abstract. 


The abstract class must be implemented in one sub class named EmpV1, implements retrieveEmp() method with JDBC code and in Emp.java one static method must be provided that returns Emp class type of object as return type.


As an when new EmpV2 or EmpV3 is produced with latest implementations like retrieving from Excel/DB/Cloud, then must change the object instantiation statement from new EmpV1() to new EmpV2() or new EmpV3();


When client requests static method client always gets latest Emp class object without his knowledge.


Client request to retrieveEmp() method always goes to latest implemented method in EmpV3 or EmpV4.


This is called as runtime polymorphism. To achieve RTP method overriding is used because Emp class method is implemented in multiple sub classes.


What are different Datastores exist?

i) Plain Text Files - RandomAccessFile

ii) Spreadsheet/Excel Files - Apache POI

iii) RDBMS (local/remote server) - JDBC

iv) XML Files - JAXP

v) JSON Files - JSONP

vi) No-SQL DB - MongoDB/Hibernate

vii) Cloud DB - Cloud DB API



emp.txt

//inside text file the document is written  in the below way

1&ABC&10000.00&Designer

2&XYZ&20000.00&Developer

3&LMN&30000.00&Tester

4&PQR&40000.00&Analyst


// Emp.java

import java.io.*;

import java.util.*;

abstract class Emp {

  private int eid;

  private String ename;

  private double sal;

  private String desig;


  public void setEid(int eid) {

    this.eid=eid;

  }

  public int getEid() {

    return eid;

  }


  protected void setEname(String ename) {

    this.ename=ename;

  }

  public String getEname() {

    return ename;

  }


  protected void setSal(double sal) {

    this.sal=sal;

  }

  public double getSal() {

    return sal;

  }


  protected void setDesig(String desig) {

    this.desig=desig;

  }

  public String getDesig() {

    return desig;

  }


  abstract void retrieveEmp();

  

  public static Emp getEmp() {

    return new EmpV7(); // Versioning implementation using Abstraction

  }

}


class EmpV1 extends Emp {

  void retrieveEmp() {

// file access

  }

}


class EmpV2 extends Emp {

  void retrieveEmp() {

// Apache POI

  }

}


class EmpV3 extends Emp {

  void retrieveEmp() {

// JDBC

  }

}


class EmpV4 extends Emp {

  void retrieveEmp() {

// JAXP

  }

}


class EmpV5 extends Emp {

  void retrieveEmp() {

// JSONP

  }

}


class EmpV6 extends Emp {

  void retrieveEmp() {

// NoSQLDB

  }

}


class EmpV7 extends Emp {

  void retrieveEmp() {

// CloudDB

  }

}


// EmpClient.java

class EmpClient {

public static void main(String rags[]) {

Emp e=Emp.getEmp();

System.out.println(e.getClass().getName());

e.setEid(1);

e.retrieveEmp();

System.out.println(e.getEname());

System.out.println(e.getSal());

System.out.println(e.getDesig());

}

}


Post a Comment

2 Comments

  1. This blogspot is very nice disinged and thanks for providing the informoation about the computer languages.

    ReplyDelete
    Replies
    1. Yeah Sure! I will make that Happen
      Thank You!
      Tech channel with dsk

      Delete