Posts

Showing posts from February, 2014

Difference between an interface and an abstract class

What’s the difference between an interface and an abstract class in Java? It’s best to start answering this question with a brief definition of abstract classes and interfaces and then explore the differences between the two. A class must be declared  abstract  when it has one or more abstract  methods . A method is declared abstract when it has a method heading, but no body – which means that an abstract method has no implementation code inside curly braces like normal methods do. When to use abstract methods in Java? Why you would want to declare a method as abstract is best illustrated by an example. Take a look at the code below: /* the Figure class must be declared as abstract    because it contains an abstract method  */ public abstract class Figure {         /* because this is an abstract method the            body will be blank  */  ...

Working with hashCode and equals methods in java

Image
In this post, i will point out my understanding about hashCode() and equals() method. I will talk about their default implementation and how to correctly override them. I will also write about implement these methods using Apache Commons package’s utility classes. Sections in this post: 1.        Usage of hashCode() and equals() 2.        Overriding the default behavior 3.        Overriding hashCode() and equals() using Apache Commons Lang 4.        Important things to remember 5.        Special Attention When Using in ORM hashCode()  and  equals()  methods have been defined in Object class which is parent class for java objects. For this reason, all java objects inherit a default implementation of these methods. Usage of hashCode() and equals() hashCode() method is used to get a unique integer for gi...