“The most basic concept of object-oriented programming, as the name suggests, is an object. Since the concept of object-oriented programming came into the picture, programming could be closer to the real world.
Here, the objects are analogous to real-life objects.
The major concepts of object-oriented programming include:
“Object is a real-world entity.”
You may look around to find various objects like pen, table, fan etc. In fact, you are also an object.
Every object has three basic characteristics:
Identity uniquely identifies an object. For e.g.: your roll number uniquely identifies you. The current condition of an object is called its state. For e.g.: a fan can have two states, on and off. The things that can be done by an object are called its behaviors. For e.g.: a pen can write, so writing is its behavior.
A formal definition of an object says – “Object is an instance of the class.”
This brings us to the second concept of object-oriented programming – ‘Class’. If we group a number of similar objects into one unit, we obtain a class.
For e.g.: a group of people like you can form a class called 'Student'. In other words, Student class has various instances (or objects) and one of them is you. Every student has certain properties like name, roll no., class etc. Also, students can perform various operations like 'get fee structure', 'pay fee', 'get result' etc.
These properties are attributes of a class and the operations are methods or behavior of a class.
If you are preparing for placement, you will come across many interview questions on object-oriented programming concepts. Here are some basic questions.
The process of grouping these attributes and behaviors into a single unit is known as encapsulation.
Now, a student can have many attributes like name, age, DOB, address, contact no., gender, height, weight, complexion, favorite food etc.
But an educational institution won’t ask for all these attributes from a student. It will ask for only necessary attributes like name, age, DOB, address, contact no. and gender. It has nothing to do with your height, weight, complexion, food preferences etc. This process of sustaining only essential and relevant information and discarding the rest is known as abstraction.
Abstraction Programming in Detail
When one class acquires the properties of another class, it is known as inheritance. The class that gives the properties is called superclass and the class that acquires the properties is called subclass.
For e.g. Animal can be a super class. Its subclasses can be Herbivores, Carnivores and Omnivores. The common properties like 4 legs can be placed in Animal class. The behavior 'eats grass' is only for Herbivores, so it cannot be kept in Animal class. However, if Herbivores inherits Animal class, the property that Herbivores have 4 legs does not need to be redefined.
The major benefit of inheritance is reusability of code.
Inheritance Programming in Detail:
The last major concept of object oriented programming is Polymorphism. ‘Poly’ means many and ‘morph’ means forms. This means it has statements that have many forms.
For e.g.: "greet" is a behavior. If you ask an Indian to greet, he will say "Namaste". An American may say "Hello". And in France, you'll come across "Salut".
This means that ‘greet’ means different things to different people. This is the concept of polymorphism.
There are two types of polymorphism:
The type depends on whether the meaning of the message is determined at compile time or run time.
These object oriented concepts are followed by most of the object oriented programming languages like C++, Java…
If you have any doubt, please ask in the comment section before proceeding for further programming tutorial.