Most of the Java learners think Classes and Objects are easy to understand, but lets revisit the concept once more and see if there is anything new to learn and explore.
The word Object is defined as something that can be felt and touched. However, Java is software programming and software cannot be touched or felt. So the first question that comes to the minds of beginners is – How have Objects come into Java?
Well, first of all, Objects in Java are not actual objects that we touch and feel but they are a conceptual entities. We use software objects to represent real world objects. we try to capture all details of a real world object into our software object.
Classes and Objects Occur in Real World
- 1 Classes and Objects Occur in Real World
- 2 Customers in Real World
- 3 Customers in Software
- 4 Classes and Objects in Object Oriented Programming
- 5 What is object-oriented?
Everything you see around yourself is an OBJECT. You write with a pen (an object), you write on a paper (an object). You read with your eyes (an object), you read text on paper / computer (an object). You drive your car (an object) on a road (an object). Basically, our entire world is made up of objects. Every software does not try to capture every detail of all objects in the world (It is practically impossible to do so!).
We create models before creating a software system. This model essentially contains all details, design, and architecture of your application on paper. The process is called Object Oriented Analysis and Design (OOAD) and is a whole new branch of study. OOAD is realized on paper through 9 specific diagrams together called UML (Unified Modelling Language), which is again out of the scope of this post.
Basically, we try to define how our system will look like, and how it will behave given the software requirements from a client. Let us define this real-to-conceptual mapping in a better way in the context of a shopping mall.
In a shopping mall, there are thousands of customers shopping for products. Let’s try to define what are classes and objects through this example.
Customers in Real World
There are thousands of customers in a shopping mall. Each and every customer is unique and this uniqueness is identified by several different things: Their height, weight, body color, how they talk, how they walk, and so many others.
We don’t have to differentiate every person in the crowd, but we end up noticing them nonetheless. Each and every customer is a real world object that looks different, have different names and wear different clothes.
Customers in Software
If we have to define a customer in a software, we follow a similar way. Humans capture details about each object they see through the object’s visible properties. A computerized representation involves pretty much the same thing – every software object has properties (or attributes) and all properties have values.
For example, all customer objects will have similar set of properties. Let’s take name, height, and skin color to be those properties. All customers will have a name, they will have some height and some skin color too.
Properties of Objects in Java
In the above example, every customer is differentiated by its properties i.e. name, height, skin color and these objects will be defined based on these properties. The values of these properties for each object may be same or may be different, but they will have these properties.
Behavior of Objects
Other than common properties, all your customers will also do something. They will move, they will talk, then can think. These are called behaviors of an object. In a software system, multiple objects interact through each others’ behaviors.
Again, all your customers will move, talk and think. So besides properties being common, we now have behaviors of all objects that are common.
Combining properties and methods into a Class
In a software application (just like in a mall), you will have to create many such objects. Since all of them have a common set of properties, we combine them in a proper unit called class. Then it becomes easy to just reuse this structure defined in the class to create any type of customer object we want.
Classes and Objects in Object Oriented Programming
A class is defined as a blueprint / template for objects. All of the objects’ common data and common behavior combined into one material unit called as Class.
An object is defined as an instance of a class. A software object occupies certain memory to hold all the properties and behavior defined in its class.
Data members are nothing but properties which belong to your objects, methods are nothing but the behavior of the objects.
What is state of an object in Java?
Remember each property comes in a name and value pair. If we have 3 objects of a class with 3 properties, each of those objects will have properties of same name, but different value.
The collective of the name and value pairs of an object is called state of an object.
Furthermore, from our customer example, we know that it is possible for two customers to have exactly the same name, height, and color. It is very rare but it is still possible. They would still be two totally different people despite all the similarities.
When that happens in software, we say two objects have same state but different identities.
This was all about classes and objects in object oriented programming. These are very general concepts applicable across all languages that follow the OOP. The only thing left now is defining object-orientation.
What is object-oriented?
A system is called object-oriented when it satisfies all the four properties:
If a language does not support inheritance and polymorphism, it is called object-based language. Java is an object-oriented language, meaning it supports all the four features, though not in the same degree.
Java still has a few chinks in the armor because of which it has failed to achieve 100% object-oriented status. If you want to know why, check out this post on Encapsulation in Java.