Posts

Showing posts with the label overriding

Characteristics of Object.equals()

I am come across the characteristics specified for implementation of equals() method in Java Classes. There is nothing like which we don’t know, but here it is well explained. I thought of sharing the same just for recalling old Java learning days Here are the characteristics that every implementation of Object.equals() should follow Reflexive. For any non-null reference value x, x.equals(x) should return true Symmetric. For any non-null reference values x and y. x.equals(y) should return true if and only if y.equals(x)   returns true. Transitive. For any non-null references value x,y and z , it x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true . Consistent. For any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified. For any non-null reference value x, x.equals(null) ...

Covariant Return Type

One of the pillars of Object Oriented Java language is Polymorphism. Polymorphism allows two or more methods within the same class that share the same name, as long as their parameter declarations are different .  It also allows two or more methods in class hierarchy where child class can have same method name as of parent class, but their signature should be same. Here is a limitation on overridden methods, they can not have different return types. J2SE 5.0 has come up with a solution for this limitation. Onwards J2SE 5.0 new feature called “Covariant Return Types”. “As per Covariant Return Types, a method in a subclass may return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.” Consider following example 1: class ParentClass 2: { 3: public SuperValue getValue(){ 4: System.out.println(" ParentClass.getValue() "); 5: return new SuperValue(); 6: } 7: 8: } 9: 10: 11: public c...