I hope you enjoyed reading the previous blog post on the static and final keyword in Java. Well, let’s take the learning journey ahead; therefore, today we are going to discuss the OOPs concept in Java. The earlier programming language was the language of hardware or simply machine; a highly skilled programmer was needed to write a program to run on those machines. In this era, a simple analytical person who is proficient enough to understand algorithms can easily become a successful programmer. The reason being, programming languages like Java, Scala, C#, Python, etc are Object Oriented Programming language and they can be related to real-life entities. In a nutshell, OOPs concept based programming languages are efficiently utilizing objects and classes. As discussed earlier, an object is a real-world entity like a dog, chair, table, etc.
OOPS Concept in Java
Let’s discuss the OOPs concept in Java; well, it simplifies the programming approach and design paradigm of the program by simply using objects and classes.
Today we are going to have a glimpse over the following topics and later we will learn them in detail. Some of them are discussed already.
- Objects
- Classes
- Inheritance
- Aggregation
- Encapsulation
- Polymorphism
- Abstraction
- Coupling
- Association
- Cohesion
- Composition
Let’s discuss them one by one.
Objects
They are the physical and logical entity that has state and behavior. Objects in java are the instances of the class which has the address and consumes some memory. Two objects can communicate with each other and pass on the messages.
Classes
They are a logical entity and simply called the blueprint of the object. A class has the collection of objects and they don’t consume any memory.
Inheritance
We are the children of our parents and we carry some of their characteristics through genes. Similarly, when a child class access data members of a parent class then it is called Inheritance in Java. It develops an IS-A relationship. It is used to achieve runtime polymorphism.
Aggregation
It is the way to associate one object with another object and it builds a HAS-A relationship. We reuse the objects through Aggregation and it develops a weak relationship between objects. For example, a Company has employees.
Encapsulation
In real life, we sometimes take medicine in the form of a capsule. When we open the capsule then we see many thin granules like medicines are enclosed within it.
Similar to the above analogy, Encapsulation is the process to bind data and code together in a single class so that they can’t be accessed outside the world.
Polymorphism
A girl plays different roles at different stages of her life, like a daughter, a sister, a wife, a nurse, etc. Similar to that, if a task performs different activities then it is called Polymorphism. For example, a draw method in Java can be used to draw different shapes like Circle, Triangle, Square, Oval, etc.
Abstraction
There are many things which we use in real life like TV, Phone, Refrigerator, etc. We use them but we don’t know their internal processing. Hence, abstraction is hiding internal processing and only showing the functionality to the user.
In Java, abstract class and interface are used to achieve abstraction.
Coupling
It’s the level of having information of one class by another class. When a class knows everything about another class then it is called Strong coupling, whereas, interface and abstract class build weak coupling with another class. We can use modifiers like public, private, and protected to define the strength of the coupling.
Association
It is used to identify the bidirectional or unidirectional relationship of one object with another. For example, a company has many employees so it is called one to many associations.
Some of the probable association’s are-
- One to one
- One to many
- Many to one
- Many to many
Cohesion
A component of Java which performs single and well-defined task efficiently then it is termed as cohesion. For example, java.io is the highly cohesive package because it only has IO related classes, interfaces, and methods, whereas, java.util is a weakly cohesive package as it contains a mixture of unrelated classes and interfaces.
Composition
It is a distinct way to achieve association. All the child objects are always related to their parent objects and deleting parent objects will delete all the associated child objects.
How Object-based programming language is different from the OOPs concept based language?
Object-based programming languages use all the OOPs concept, except Inheritance. Example: JavaScript, VBScript.
This was all about the OOPs concept in Java. Feel free to connect with us and stay tuned for more concepts on Object Oriented Programming System. Do not miss to subscribe to our Facebook group.