Posts

Showing posts from 2015

Difference between "== operator" and "equals() method"

Image
== Operator: The "== operator" is used to compare primitives types and objects. While comparing primitives types like int, float, boolean "==" operator works fine. The "== operator" compare two objects based on memory reference . So "==" operator will return true, only if two references which it is comparing is pointing to the exactly same object otherwise "==" will return false. equals() Method: The equals() method has been defined in java.lang.Object class and used for comparing two objects on the basis of their contents. In java, if two objects are equal they should have equal hashcode as well. The equals() method has contract with hashcode() method in java that whenever you override equals() method you should also override the hashcode() method.  Default implementation of equals() method is similar to the == operator which returns true if you are comparing two references of same object. D

Method Overloading Vs Method Overriding

Method Overloading: A feature that allows a class to have two or more methods having same name but different argument is said to be Method Overloading . Two methods are said to be overloaded if and only if both methods having the same name but different argument types. Also known as Static Binding , Compile-Time Polymorphism and Early Binding. class Calculate { public void area(int a) {//circle System.out.println("Area of Circle = " + (3.14 * a * a)); } public void area(int l, int b) { //rectangle System.out.println("Area of Rectangle= " + (l * b)); } } In above example, we have created two overloaded methods, first area() method prints area of circle and second area() method prints area of rectangle. Method Overriding: The methods declared in parent class are by default available in the child class through the inheritance . But sometimes child may not be fully satisfied with the parent class method implementation, then the child is allow