Wednesday, April 21, 2010

== operator vs equals() method

First of all we have to understand about == operator.
The figure shows that there is two object of Student class.
class Student{
int id;


There is two reference variable s1 and s2 referring to the same object of memory address 1001, i.e. the value of reference variable s1 and s2 1001. While, s3 is referring to different object with memory address 1010.
If we compare s1==s2 it will return true because both are referring to the same object.

However, the object on memory address 1010 having the same id (id=1) as referred by s1 even s1==s3 or s2==s3 will return false because they posses different memory addresses i.e. they are referring to two different objects.
It means, == operator compare between the value of reference variable rather than the content of the object.
Now, come to the equals(). This method is defined within Object (the super cosmic) class. In Object class the equals() method is defined in such way that it works just same as == operator. It means, equals() method provided by Object class also compares the value of reference variable instead of content of objects.
But it provides us the facility that we can override the equals() method and compare two objects on the basis of our requirement, i.e. to compare two object on the basis of its content, we can override equals().
e.g. in above Student class we can override equals() like
public boolean equals(Object obj) {
if (obj == null) {
return false;
if (getClass() != obj.getClass()) {
return false;
final Student other = (Student) obj;
if ( != {
return false;
return true;
and now if we compare between s1, s2 and s3, if id of object is same then equals() method will return true but s1==s3 or s2==s3 will return false.

Friday, April 16, 2010

Create Executeble jar file

Step 1: Create one .mf file in notepad.
 e.g. and write following with one blank line
 (i) Manifest-Version: 1.0
 (ii) Main-Class: packageName.MyBeginingClass
Step 2: jar cvmf jarFile.jar *.class

Amit Ranjan
Java developer
P please consider the environment before printing this e-mail-- SAVE PAPER!

Disadvantage of inheritance

i. Main disadvantage of using inheritance is that the two classes (base and inherited class) get tightly coupled.
ii. This means one cannot be used independent of each other.
iii. Also with time, during maintenance adding new features both base as well as derived classes are required to be changed.
iv. i.e. If a method signature is changed then we will be affected in both cases (inheritance & composition)

v. Either of both of them are changed to accommodate changes.
vi. This further makes them more tightly coupled and lots of contextual information tends to be added.
vii. If a method is deleted in the "super class" or aggregate, then we will have to re-factor in case of using that method.Here things can get a bit complicated in case of inheritance because our programs will still compile, but the methods of the subclass will no longer be overriding superclass methods. These methods will become independent methods in their own right.

Thursday, April 15, 2010

Abstraction vs Encapsulation

A simple definition of abstraction is to hide actual implementation of an object from the external world that would use the object and just exposing the functionality.
For example,
i. A program that is drawing circles and squares using those objects need not know how those objects are implemented. It is enough for the program to know what the behavior of these objects is, and how to use these objects (rather than how these objects are implemented internally).
ii. When you change the gear of your car, you know the gears will be changed without knowing how they are functioning internally. Abstraction focuses on the outside view of an object (i.e. the interface)
iii. Taking one another example of electric switch board in our house. Wires inside the switch board are encapsulated (hidden) and switches are like methods using which we can put-on/off the bulb/fan. And the process behind the switch-on/off event is abstracted, i.e. we can never know, how bulb/fan is working when we put-on the switch.
iv. The benefit is that you don't need to deal with the (potentially big and complicated) inner workings of the object in order to use it. So you are protecting the user from the object.
i. Combining the data(properties/attributes/information) and the methods (behaviors of class with the data) that can manipulate that data into one capsule (class/object) with guarantees that the encapsulated data is not accessed by any other function/method outside the encapsulated object and must follow the programmer's/ owner's strategy regarding manipulation of data members.
ii. In other words, Encapsulation means put the data and the function that operate on that data in a single unit (information hiding) which prevents clients from seeing it's inside view i.e. clients can view data by the interface given by the programmer by means of methods only.
iii. Such methods hold the behavior of the abstraction (i.e. hide the answer of how it works) is implemented.
iv. Encapsulation can be achieved by using proper access modifiers like, public, private, etc to control the scope of the variables.
v. Encapsulation promotes separation of state of an object from its behavior.Hence, we can say that when we encapsulate data and methods that operate on data into one object, the external program that uses this object need not know the internal workings of the object to use the object. Thus is making the object abstract data type to the external program.

Plz. click any one advertisement shown on this blog to manage this blog.