Q3. Explain the basic concepts of OOP with examples.
Ans. The object oriented programming has been developed with the view to overcome the drawbacks of traditional programming approaches. The OOP approach is based on certain concepts that helps it attain goal of overcoming the drawbacks or shortcomings of traditional programming approaches. These general concepts of OOP are given below:
- Data Abstraction
- Data Encapsulation
- Data Abstraction:
Abstraction is the concept of simplifying a real world concept into its essential elements.
Abstraction refers to the act of representing essential features without including the background details or explanations.
Let’s take the example of ‘switch board’. You only press certain switches according to your requirement. What is happening inside, how it is happening etc. you needn’t know. This is abstraction, you know only the essential things to operate on switch board without knowing the background details of switch board.
Encapsulation is the most fundamental concept of OOP. It is the way of combining both data and the functions that operate on that data under a single unit.
The wrapping up of data and operations/functions (that operate on the data) into a single unit (called class) is known as Encapsulation.
Let us now consider an analogy to encapsulation. In a big company, there are so many departments, Sales, Accounts, Payroll, Purchase, Production etc. each department has its own personnel that maintains its data. Suppose an employee in the production dept. wants to know how much raw material has been purchased for the next month. The production dept would not be allowed to himself go through the purchase dept. data files. Rather he’ll have to issue a memo to the ‘purchase’ requesting for the required information. Then some employee of the ‘purchase’ dept. will go through the purchase data files and send the reply with the asked information. This practice ensures that the data is accessed accurately and that it is not corrupted by inept outsiders. Therefore, we can say here ‘Department data and department employees are encapsulated into a single entity, the department’. In the same way, objects provide an approach to program organization while helping to maintain the integrity of the program data.
Modularity is the property of a system that has been decomposed into a set of cohesive and loosely coupled modules.
Following are the benefits offered by modularity:
- It reduces program’s complexity to some degree.
- It creates a number of well-defined, documented boundaries within the program.
For example, you must have seen a complete music system. Let us assume that our program represents the music system. The music system comprises of speakers, cassette-player, record-player, cd-player, tuner etc. now, these parts are complete units in themselves, yet they are a subpart of the music system. This is modularity. Similarly, we can achieve modularity in programs also. A program can be divided into various modules each representing speakers, cassette-player, … etc. see each module is a complete unit in itself yet it works in accordance with other modules in order to achieve one single goal, that is, music.
Inheritance is the capability of one class of things to inherit capabilities or properties from another class.
For instance, we are humans. We inherits from the class ‘Humans’ certain properties, such as ability to speak, breathe, eat, drink etc. etc. But these properties are not unique to humans. The class ‘Human’ inherits these properties from the class ‘Mammal’ which again inherits some of its properties from another class ‘Animal’.
Polymorphism is the ability for a message or data to be processed in more than one form.
Polymorphism is the concept that supports the capability of an object of a class to behave differently in response to a message or action. For instance, ‘Human’ is a subclass of ’Mammal’. Similarly ‘Dog’, ’Cat’, are also subclasses of ‘Mammal’. Mammals can see through day-light. So if a message ‘see through daylight’ is passed to all mammals, they all will behave alike. Now if a message ‘see through darkness’ is passed to all mammals, then humans and dogs will not be able to view at night whereas cats will be able to view during night also. Here cats (mammals) can behave differently than other mammals in response to a message or action. This is polymorphism. ’