Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Sunday, March 12, 2017

Object in Java

Object in Java


In java we can define object as the instance of class. Object is used to instantiate all the non-static members. In java everything is based on Object, it means memory are allocating for the objects only. Everything we are dealing with is the help of object only , it may be calling method, accessing instance variable and many more. 

Important point regarding Object in java

1> Object creation is with the use of new operator.

2> All the objects are created in the heap memory which is a part of main memory.

3> Whenever we are creating JVM will first call to default constructor if no constructor in defined in the class to initialize the instance variable. 

4>We can define object for any class, it may be predefined class or use-defined class. 
For ex-There is predefined classes available  for every primitive data type in Object class so that we can use create object for primitive type as well called as wrapper class.

Syntax for defining Object :
< class_name >   <reference_variable >  =  new  < class_name >( [value] ) ;
for ex- Hello h = new Hello( ) ;

In the above syntax we can see that class_name is user defined class and with the new operator we had defined a constructor of the class where we can pass value as well but it is optional. 

5> Reference value is always pointing to the object of the class with contain the reference value where the object is stored in the memory.

6> If not object is created then reference variable is containing NULL as the reference value indicating not pointing any object.

Let me make you more understood with the help of an example :

public class prog{

int x ; // instance variable

String name ; // instance variable

long ln ; // instance variable

float f ; // instance variable

void display(){ // Instance method

System.out.println("Display method");

}


public static void main(String sush[ ]) {

prog p = new prog( ); // Object created

p.display(); // Instance method called

    }
}

Here variable " x ", " ln "," f " and " name " is instance variable and display ( ) is an instance method . It is only called with the help of object only. 

Here we create and object in the main ( ) by which JVM will internally call a default constructor which is responsible for initializing instance variable with the default value if not initialized by the programmer manually. 

Diagrammatic representation of Object 

Representation of an Object
Here we can see that as per above example " p " is a reference variable indicating the object which contain the instance variable stored in the heap memory.




You all must be wondering that what happened to instance method  ?
Instance method will be called by the object and get executed in the stack memory always. And it is only executed when we are calling method manually in our program with the help an object otherwise it wont execute self. 

Note : If we are not using any new operator then it will remain as a reference value which store null, means not pointing any object.

Available link for download

Read more »

Friday, March 10, 2017

Object Relational Mapping

Object Relational Mapping


Over the years we have seen a paradigm shift in Programming Languages from the traditional Procedural programming approach to an Object-Oriented approach. However Databases have changed very little in terms of their fundamental principle - Set Theory. Databases are and have been Relational almost from their advent. Most of the Database Management Systems which we use like MySQL, Microsoft SQL Server and Oracle are Relational to a very large extent. Though there have been approaches like Object-Relational DBMS and Hierarchical DBMS, they have rarely been adopted in production environments.

This article focuses on creating a relationship between Object-Oriented Programming Languages and Relational Databases through a concept called Object-Relation Mapping. This article will be followed by two related articles - Using Hibernate in Java with NetBeans and Using ADO.NET Entity Framework in .NET (C#) with Visual Studio.

Before jumping into ORM, here are two common concepts which most of you must be familiar with -

Class - A class is a construct that is used as a blueprint (or template) to create objects of that class. A class defines the properties that each and every object possesses.

Table Schema - A table schema primarily defines the fields and relationships of a table. A table contains records which have the same structure.

Observing closely, we can notice that these two definitions are pretty similar. Both of them talk about a template which multiple instances follow (objects in OO and records in DBMS). Both of them talk about fields (properties) these instances possess.

This similarity is the basis of ORM. Table Schemas of Relational Databases correspond to Classes in an Object Oriented Programming Language and Records of these tables correspond to instances of the Classes (Objects).

Consider an example of a Student table in a Database created with the following schema -

CREATE TABLE Student_tbl
(
StudentId INT PRIMARY KEY,
Name VARCHAR(MAX),
Age INT
)

This table can be translated into a Class with the following structure -

class Student
{
Int32 StudentId;
String Name;
Int32 Age;
}

Each record of the Student_tbl will be an object of the Student class.

There are several free and commercial packages for Object Oriented languages that perform Object Relational Mapping. Most of these packages incorporate advanced features like -

  • Automating the class generation process from the Database Schemas
  • Maintaining foreign key dependencies using Lists
  • Generating identity keys while inserting records and using these keys in subsequent insertions as foreign keys if required
  • Creating methods for retrieving data and saving data directly as objects

The major advantages of ORM lie in -

  • Minimal database dependency - most of the ORM packages use a concept called Dialect to identify the DBMS the application is connecting to. So changing the dialect when the DBMS is changed is sufficient for the application to run. No application code has to be changed
  • ORM reduces the amount of code that needs to be written by a developer

However it is often argued that ORM packages dont perform efficiently during bulk deletions and with joins. So generally it is recommended to check if there a hit in the efficiency of the application when ORM tools are introduced, especially when complex operations are involved.

Though ORM is a simple concept, its a rapidly over-shadowing the traditional database connectivity models in Object Oriented Programming Languages like Java and C#. In my next post, I will be introducing Hibernate - an ORM package for Java and in the subsequent post I will introduce the ADO.NET Entity Framework - an ORM package for .NET.


Available link for download

Read more »