Monday, 6 January 2014

Prototype Language

Most of us are thinking that JavaScript is object oriented programming language.Actually it is not. we will get confuse when we say Java is object oriented programming language and JavaScript is prototype based language. lets see the difference between them.

In Object Oriented Programming language everything will be represented as classes and Instances. Prototype based language is in which behaviour reuse (known as inheritance) is performed via a process of cloning existing objects that serve as prototypes.

For example "Fruit" is an object. It represents the properties and general functionalities of fruit. Another object "Strawberry" would be cloned from "fruit" object  and would also be extended to include general properties specific to bananas.
In prototype-based languages there are no explicit classes and objects inherit directly from other objects with which they are linked through a property, often called prototype as in the case of Javascript.

Equally equals the equal() method !!

Equals and hashcode are very important in Java. Its better to know about equals method in detail so that we can make use of it properly.I have shared here some details about equals in java that I have known. Please do read other websites also for further clarification.
Equals is the method which is implemented by Object class(Super class of all classes). Equals will compare the object reference. If equals method is not overidden then it will compare the reference of object. The following are some comparison concepts

-- Lets consider a and b as an object of some class.

Reflexive : Object must be equal to itself.
Symmetric : a.equlas(b) - return true then b.equals(a)// if a object refernce is same as the b object reference
Transitive : a.equals(b) - return true, b.equals(c) - return true then a.equals(c) will return true. // a,b,c object refernce are same
Consistent : If two object reference are equal then it will be equal until any of their properties are changed.
Null comparison: a.equals(null) -return false && null.equals(a) -null pointer exception // If object is compared with null then it will return false.If null is compared with object then it will throw nullpointerexception.

EQUALS AND HASHCODE :
--------------------------------
* If two obecjts are equal then Hashcode must be same of those objects.
* If two objects are not equal then Hashcode may be same or may be not.
-------------------------------------------------------------------------------------------------------
Always override equals method is advisable
-------------------------------------------------------------------------------------------------------

So this was the basic theory about equals method in Java now we are going to discuss the approach on how to override equals() method
Overriding Equals Method: This is the standard overriding equals method which is followed by most of Java programmers.
Before Overriding equals method its better to check the following conditions

1) Check if object is Null
2) Check this is equal to object if yes return true
3) instanceof method will return true though subclass object is given for comparison. So better go for getclass() method.
-----------------------------------------------------------------
if((obj == null) || (obj.getClass() != this.getClass()))
return false;
-----------------------------------------------------------------
Once you checked all conditions then type cast object into your class.Then compare each instance variable of the object are or not. Here is the example of overriding equals method.

-------------------------------------------------------------------------------------
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || obj.getClass() != this.getClass()) {
return false;
}

Person guest = (Person) obj;
return id == guest.id
&& (firstName == guest.firstName
|| (firstName != null && firstName.equals(guest.getFirstName())))
&& (lastName == guest.lastName
|| (lastName != null && lastName .equals(guest.getLastName())));
}
--------------------------------------------------------------------------------------

Errors we can expect while overriding:
1) Instead of overriding,most of people will do overload. Equals method takes an argument as an Object datatype. While overriding there are chances of giving parameter type as direct class name. For example, public boolean equals(Person obj).
2) Some times we will forget to do casting and we will start comparing the instance variable. For Example, we may forget to do person guest=(Person)obj.
3) Its not enough to check only object is null or not. We also need to check instance variable is null or not. If any instance variable is null then there is a chance of null pointer exception. For example,
firstname == guest.firstname || (firstname != null && firstname.equals(guest.firstname)));
4) If equals method is overriden then hashcode method is also to be override. Else Hashmap will not function properly. Hashmap is based on key pair value. Hashmap works on both equals and hashcode method.

TIPS :

1) If your class has any unique instance variable then in equals method its enough to compare that unique instance variable. For example, In person class all names are unique then its enough to compare personName variable in equals method.
2) equals method are safe in Immutable class than in mutable class.
3) Wrapper classess such as String, Integer, Float etc equals method can be overriden but equals method of StringBuffer class can't be overriden