1) Primary emotions
2) Secondary emotions
Cheers !!!
Vinay Mudgil ---- News and Views
The most successful people in any walks of lives may not be the most talented ones but are definately the most passionate & dedicated ones.
Object
class simply checks if two object references x and y refer to the same object. i.e. It checks if x == y
. This particular comparison is also known as "shallow comparison". However, the classes providing their own implementations of the equals
method are supposed to perform a "deep comparison"; by actually comparing the relevant data members. Since Object
class has no data members that define its state, it simply performs shallow comparison.equals
method to behave otherwise.equals
method that has comparison with an object of java.lang.String
class, or with any other built-in Java class for that matter. It is very important to understand this requirement properly, because it is quite likely that a naive implementation of equals
method may violate this requirement which would result in undesired consequences.equals
method in such a way that it provides comparison for objects of class A and class B. Now, if author of class B decides to modify its equals
method such that it would also provide equality comparison with class C; he would be violating the transitivity principle. Because, no proper equals
comparison mechanism would exist for class A and class C objects.public int hashCode()
equals
method.hashCode
is:1. public class Test
2. {
3. private int num;
4. private String data;
5.
6. public boolean equals(Object obj)
7. {
8. if(this == obj)
9. return true;
10. if((obj == null) || (obj.getClass() != this.getClass()))
11. return false;
12. // object must be Test at this point
13. Test test = (Test)obj;
14. return num == test.num &&
15. (data == test.data || (data != null && data.equals(test.data)));
16. }
17.
18. public int hashCode()
19. {
20. int hash = 7;
21. hash = 31 * hash + num;
22. hash = 31 * hash + (null == data ? 0 : data.hashCode());
23. return hash;
24. }
25.
26. // other methods
27. }
num
and data
. These two variables define state of the object and they also participate in the equals
comparison for the objects of this class. Hence, they should also be involved in calculating the hash codes of this class objects.equals
method first. We can see that at line 8, the passed object reference is compared with this
object itself, this approach usually saves time if both the object references are referring to the same object on the heap and if the equals comparison is expensive. Next, the if
condition at line 10 first checks if the argument is null
, if not, then (due to the short-circuit nature of the OR ||
operator) it checks if the argument is of type Test
by comparing the classes of the argument and this object. This is done by invoking the getClass()
method on both the references. If either of these conditions fails, then false
is returned. This is done by the following code -if((obj == null) || (obj.getClass() != this.getClass())) return false; // prefer
if(!(obj instanceof Test)) return false; // avoid
false
if the argument is a subclass of the class Test
. However, in case of the second condition (code in red) it fails. The instanceof
operator condition fails to return false
if the argument is a subclass of the class Test
. Thus, it might violate the symmetry requirement of the contract. The instanceof
check is correct only if the class is final
, so that no subclass would exist.equals
method correctly.==
operator to check if the argument is the reference to this object, if yes. return true. This saves time when actual comparison is costly.null
and it is of the correct type, if not then return false
.if((obj == null) || (obj.getClass() != this.getClass())) return false;
ClassCastException
.==
) after performing any necessary conversions (Such as float to Float.floatToIntBits
or double to Double.doubleToLongBits
). Whereas, object references can be compared by invoking their equals
method recursively. You also need to ensure that invoking equals
method on these object references does not result in a NullPointerException
, as shown in the example above (Line 15).equals
method. Only you can decide which class members are significant and which are not.equals
method. It takes a java.lang.Object
as an argument, do not use your own class instead. If you do that, you will not be overriding the equals
method, but you will be overloading it instead; which would cause problems. It is a very common mistake, and since it does not result in a compile time error, it becomes quite difficult to figure out why the code is not working properly.equals
method to verify that it fulfills all the requirements stated by the general contract of the equals
method.hashCode
method whenever you override the equals
method, that's unpardonable. ;)These terms signify the relationships between classes. These are the building blocks of object oriented programming and very basic stuff. But still for some, these terms look like Latin and Greek. Just wanted to refresh these terms and explain in simpler terms.
Association
1) Association is a relationship between two objects. In other words, association defines the multiplicity between objects.
2) Different types of association between objects are:- one-to-one, one-to-many, many-to-one, many-to-many.
3) Example: A Student and a Faculty are having an association.
Aggregation
1) It is a specific case of association.
2) Aggregation is also called a “Has-a” relationship i.e. When an object ‘has-a’ another object, then you have got an aggregation between them.
Composition
1) Composition is a special case of aggregation.
2) When an object contains the other object, if the contained object cannot exist without the existence of container object, then it is called composition.
Difference between aggregation and composition
1) When there is a composition between two objects, the composed object cannot exist without the other object. Composition is more restrictive. This restriction is not there in aggregation.
2) In aggregation, though one object can contain the other object, there is no condition that the composed object must exist. The existence of the composed object is entirely optional.
3) In both aggregation and composition, direction is must. The direction specifies, which object contains the other object.
4) Example: - Let analyze Library, Student and Books.
Relationship between library and student is aggregation because a student can exist without a library and therefore it is aggregation.
Relationship between library and book is composition because A book cannot exist without a library and therefore its a composition.
In terms of coding snippet example of difference between aggregation and composition
Aggregation Example: the object exists outside the other, is created outside, so it is passed as an argument (for example) to the constructor. Ex: People – car. The car is created in a different context and then becomes a person’s property.
****************************************************************************
// WebServer is aggregated of a HttpListener and a RequestProcessor
public class WebServer
{
private HttpListener listener;
private RequestProcessor processor;
public WebServer(HttpListener listener, RequestProcessor processor)
{
this.listener = listener;
this.processor = processor;
}
}
****************************************************************************
Composition Example: the object only exists, or only makes sense inside the other, as a part of the other. Ex: People – heart. You don’t create a heart and then passes it to a person.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
// WebServer is an composition of HttpListener and RequestProcessor & controls their lifecycle
public class WebServer
{
private HttpListener listener;
private RequestProcessor processor;
public WebServer()
{
this.listener = new HttpListener(80);
this.processor = new RequestProcessor(“/www/root”);
}
}
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Here the key difference [wrt code snippet above] to note is that :-
In Aggregation we are passing the already instantiated objects [ HttpListener and RequestProcessor ] from outside the Webserver class so they exist independently of the Webserver class though Webserver class shares “has-a” relationship with them.
In Composition we are instantiating the classes [ HttpListener and RequestProcessor with in the Webserver class so they are dependent on Webserver class. Here also Webserver class shares “has-a” relationship with them.
Abstraction
1) Abstraction is specifying the framework and hiding the implementation level information. Concreteness will be built on top of the abstraction.
2) It gives you a blueprint to follow to while implementing the details. Abstraction reduces the complexity by hiding low level details.
3) Example: A wire frame model of a car.
Generalization
1) Generalization uses a “is-a” relationship from a specialization to the generalization class.
2) Example: (a) Consider there exists a class named Person. A student is a person. A faculty is a person. Therefore here the relationship between student and person, similarly faculty and person is generalization. (b) Lets have specialization class [Triangle and Circle] and generalization class [shape] so the relationship b/w specialization to generalization class is that every Square “is-a” Shape.
Realization
1) Realization is a relationship between the blueprint class and the object containing its respective implementation level details. This object is said to realize the blueprint class.
2) In other words, you can understand this as the relationship between the interface and the implementing class.
Dependency
Change in structure or behavior of a class affects the other related class, then there is a dependency between those two classes.
Please feel free to disagree (if you find anything wroing) on the above explanations and believe me you won't be the first one. Do spare me for any typo's above (if any). Happy-to-learn !!!
Cheers,
Vinay