Object oriented programming languages like Java focuses on modeling of objects for developing an application. These objects can be defined by 2 characteristics : state and behavior. Here, let’s understand this with the help of an example.
Suppose, we are developing an application related to online shopping portal. If we try to identify the real life objects here, perhaps the first object that comes to our mind would be “customer”. This object “customer” would have a state defined by properties like name, address, e-mail id, phone number etc. Object “customer” also has some behavior like placing order, maintaining a wishlist, tracking order etc. Now, to map this real life object to a sofware object we can define state using variables and behavior by implementing methods.
While modeling a real life scenario for implementation, we should try to make these classes as cohesive as possible. To understand this in simple language, we can say that one class should not be dependent on another and should be designed by keeping one focused purpose in mind. If our classes are less dependent on each other, it will definitely be very easy to maintain the application.
Next, let’s talk about one concept of OOPS called “encapsulation”. Above, we talked about defining an object using it’s state and behavior. A class of an object is implemented by combining state, represented by variables and behavior, represented by methods. This concept of combining is known as encapsulation. This provides a benefit of protecting object variables from unauthorized access as we can implement the methods to access variables that we do not want to make accessible for any code. Here, one more concept of “loose coupling” comes into the picture which is related to design rather than coding. In a way, coupling describes how one class is dependent on another class. When there is less dependency, it is considered as loose coupling, which is recommended. Therefore, while designing, it is very important to identify different object as per their state and behavior.
Let’s talk a little bit about Java class. A class can be defined as a blue print for object which defines the characteristics for the objects of that class. It is more like a template that can be used to create similar objects of that class. e.g. A shopping portal may have many customers but with different name, address etc. So we can create a class Customer and create as many objects as required but with different state/characteristics (represented by variables).