Introduction To JavaScript Object-Oriented
An object-oriented programming system (OOPs) is a programming model that works on the concept of an object. Here the design of the program is intended to work with characteristics of the object rather than function or logic. Below we discuss JavaScript Object-Oriented.
The object is defined as an information set that has specific features or behavior and the objects can be divided by class. For example, a human being can be called an object. Human beings can be either male or female, thus this refers to class. Walking, running, talking, etc. these refer to the behavior of objects which both classes can perform but at the same time, some specific features like giving birth to a child cannot be a feature for the male class.
Classes represent broad classifications, like for example car that shares some properties. These classes describe what qualities type will have like color, brand, fuel type. For example, consider class Car, it must-have color, brand, and model. Now, create an instance of a Car type object myCar to represent my car and elonCar to represent another car. Now we can specify our requirements of color, type, and brand without actually describing everything. We can use the same class to define another car as well. These classes act as templates in which we define our features as per requirements. This template can be used to specify any number of cars.
The below image gives the illustration of class and features. JavaScript uses the OOPs principle.
Characteristics Of Object
OOP provides these characteristics to an object in JavaScript Object-Oriented:
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
1. Abstraction
This refers to hiding the complex and huge details of the mechanism of the object and showing only the simple and easy mechanism in front. These will help to avoid large changes in the program. For an instance take the example of ATM. Our work is to just insert a card, input pin, and get the cash, it seems so simple, but when you see what is happening behind the machine is a complex mechanism, security systems, logics, etc. The person with drawing money need not know all these and it’s hidden, the same thing will be applied to objects and this feature is known as abstraction.
2. Encapsulation
It means that all the important information is inside the object and it exposes only selected information to the environment. When an object is defined, data and methods are encapsulated in it. Encapsulation makes the complex system easier for the end-users. For an instance take the example of the car door. To open the door, the user just needs to lift the handle and the door opens. But, in the background, the latch (door locking system) is a very complex mechanism that synchronizes with the mechanism. These things, users need not worry and just lift the handle. The main difference between abstraction and encapsulation is that abstraction focuses on essential data and hides the rest. In encapsulation, it makes users use complex systems in a simple way and avoid details.
3. Inheritance
Inheritance refers to that all the properties the parent possesses will be inherited by children. This property helps to reuse the codes for objects with similar properties. There will be some objects which share a common logic but are not completely the same. Thus, to make it easier the inheritance feature will help to reuse the codes. For an instance, we can take an example of planets revolving around the sun. When we say planet mars, it revolves around the sun but at the same time, all the things which revolve around the sun are not planets. Comets, asteroids, satellites are not planets. Thus here, the “revolving” feature is inherited to all the “planets”.
4. Polymorphism
This refers to apply the same features in different objects or in other words objects behave differently for the same feature. For example: in a car, there are 5 gears, 4 for forward and 1 reverse. Here applying gear is the feature and the car moving forward gives 1 form and going reverse gives another form.
Models In OOPs
The 3 models of OOP methods are:
- Class Model
- State Model
- Interaction Model
1. Class Model: Class model describes the classes present in the system. The class diagram shows the class name followed by features followed by methods. The goal of this model is to capture perceptions from the real world that are essential for the application.
2. State Model: This defines the object’s activity at a particular time or sequence of operation. In this model, the activity’s operation is not considered and only the process is considered.
3. Interaction Model: This describes interactions between the objects and how they communicate and collaborate to achieve the objective of the system. It uses a case diagram, sequence diagram, and activity diagram to represent the interaction model.
Conclusion – Is JavaScript Object-Oriented?
This article helps to know about the Object-oriented programming system and their models along with characteristics.
Recommended Articles
This is a guide to Is JavaScript Object-Oriented? Here we discuss the introduction to JavaScript Object-Oriented and its characteristics along with models. You can also go through our other suggested articles to learn more –
Are you preparing for the entrance exam ?
Join our Programming Languages test series to get more practice in your preparation
View More