Static Method
• Belongs to the class, not any particular instance.
• Can be called without creating an object of the class.
• Can access static data members and change their values.
Instance Method
• Belongs to an instance of the class.
• Requires an object of the class to be called.
• Can access instance variables and methods directly.
Abstraction is the concept of hiding the complex implementation details and showing only the essential features of the object.
Polymorphism is the ability of an object to take on many forms. It allows methods to do different things based on the object it is acting upon.
Abstract Class
• Can have both abstract and non-abstract methods.
• Can have instance variables.
• Can provide method implementations.
Interface
• Can only have abstract methods (Java 8 onwards, can have default and static methods).
• Cannot have instance variables.
• Provides a way to achieve full abstraction.
Inheritance is a mechanism where one class acquires the properties and behaviors of a parent class.
Class Object
• The root class of the Java class hierarchy.
• Every class in Java is a subclass of Object.
• Provides basic methods like equals(), hashCode(), toString(), etc.
Class Objects
• A utility class in java.util package.
• Provides static methods to operate on or return objects.
Encapsulation is the technique of wrapping the data (variables) and code acting on the data (methods) together as a single unit. It restricts direct access to some of an object’s components.
OOP (Object-Oriented Programming)
Object-Oriented Programming is a paradigm centered around objects and classes. The four main principles of OOP are:
• Abstraction: Hiding the complex implementation details and showing only the essential features.
• Polymorphism: The ability of an object to take on many forms, allowing methods to do different things based on the object it is acting upon.
• Inheritance: A mechanism where one class acquires the properties and behaviors of a parent class.
• Encapsulation: Wrapping the data and code together as a single unit, restricting direct access to some of the object’s components.
hashCode()
The hashCode() method returns an integer hash code value for an object. It is used in hashing-based collections like HashMap, HashSet, and Hashtable to store and retrieve objects efficiently.
When you override the hashCode() method, you ensure that objects which are equal (according to the equals() method) produce the same hash code. This is crucial for maintaining the contract between hashCode() and equals().
equals()
The equals() method checks if two objects are equal. By default, the equals() method in the Object class checks for reference equality (i.e., whether the two references point to the same object).
When you override the equals() method, you can define what it means for two objects of your class to be considered equal. Typically, this involves comparing the values of relevant fields.
Example
Consider a class Person with fields name and age. To properly use objects of this class in a HashSet, you need to override both equals() and hashCode().
Summary
• Override equals() to define when two objects should be considered equal based on their state.
• Override hashCode() to ensure objects that are equal according to equals() return the same hash code, facilitating correct behavior in hash-based collections.
• Proper implementation of both methods is crucial for objects used in collections like HashMap, HashSet, etc.