This tutorial gives you knowledge of various object orientation concepts like object, class, message, object state and principles of object orientation.
What is the need to learn about object orientation concepts?
As you have already seen here: introduction to java, Java is an object oriented programming language. Java follows or is built upon the object oriented paradigm. Generally, every programming language supports at least one of the programming paradigms.
To understand a Java program and its structure, it is essential to know about the object orientation concepts.
Structured Paradigm Vs Object Oriented Paradigm
In programming languages following structured paradigm or procedural paradigm instructions are written in linear blocks or using functions. We can say that in procedural or structured paradigm, “Code allows access to data”. Examples of programming languages following procedural or structured paradigm can be found here.
In object oriented paradigm, the program is written using objects and classes. A problem can be solved through interactions between objects. We can say that in object oriented paradigm, “Data allows access to code”. Examples of object oriented languages can be found here.
Object Oriented Paradigm
The fundamental unit in object oriented paradigm is an object. Let’s look at an object in detail.
Object is a real world thing or entity that is tangible or intangible. An object consists of properties and/or behavior. For example consider a student object whose name is John. Student object contains properties or characteristics like name, age, gender, date of birth etc and behavior like eat, sleep, register, write exam etc.
A student object can be represented on paper or electronically as shown below. I am using the industry standard language UML (Unified Modeling Language) for representing the student object.
It is common for most of the Java programs to have similar objects. Similar means objects having same properties and behavior. So, a class can be defined as a mold or template for creating objects. A class can also be defined as a collection of similar objects.
In the above example, the class is the mold (in yellow color) and the object is the paw (blue color). All the objects (blue color) will have the same size, shape etc.
Considering the above student example, let’s think of three student objects: John, Mary and Krishna. All of these three students will have similar properties like name, age, gender etc and similar behavior like register, write exam etc. These three objects can be grouped into a class named Student. A class can be represented diagrammatically (using UML) as shown below:
Now we can represent the class Student and the three objects John, Mary and Krishna of the class Student as shown below:
By now you will be able to distinguish between an object and a class. There is some more terminology you must know. Above, I said that object contains properties and behavior.
A property is a characteristic which can be used to describe the object and will generally be a noun. Behavior is defined as a set of operations that can be performed by the object or on the object. Operations are generally verbs.
The set of values assigned to the properties of an object is known as the state of an object. For example we know that for the John object there are three properties: name, age and gender. Then, the state of John object will be John, 21 and male which are the values of name, age and gender respectively.
In object oriented world objects don’t sit idle. They communicate with one other by passing messages. A message is a request to carry out a task. A task can be anything from displaying a message to retrieving data from database and process it.
An object can send messages to those objects only which can process the messages. For example, let’s think that an object Ken of Person class wants his dad (John) to purchase apples for him. To carry out that task Ken sends the message getApples(10) to John. This can be viewed diagrammatically as shown below:
It is the responsibility of John object to process the message getApples(10) and give response back to Ken object. The response message is not mandatory. In the above message getApples(10), 10 is a parameter.
A parameter is a mechanism to pass additional information along with the message. In the above example, 10 reflects the number of apples Ken wants.
Along with the above key concepts, there are key principles which act as the foundations in object oriented paradigm. Click this link to know about them: principles of object orientation.