Object Oriented Programming With A Real-World Example

3


In interviews, young programmers are asked for a real-world scenario explaining OOP and many fail to answer. This is the reason I am writing this article. This article is mainly intended for the audience who knows the Object-Oriented Programming (OOP) concept theoretically but are unable to link it with the real-world & programming world.


Object-Oriented Programming is considered a design methodology for building non-rigid software. In OOPS, every logic is written to get our work done but represented in form of Objects. OOP allows us to break our problems into a small unit of work that is represented via objects and their functions. We build functions around objects.
 
There are mainly four pillars (features) of OOP. If all of these four features are presented in programming, the programming is called perfect Object-Oriented Programming.
  1. Abstraction
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
Let's consider an example explaining each of these pillars so you can better understand Object-Oriented Programming.
 
Before that, we need to know something. 
 
When we think of a mobile phone as an object, its basic functionality for which it was invented were Calling & Receiving a call & Messaging. But nowadays thousands of new features and models were added and the features and number of models are still growing.



In the above diagram, each brand (Samsung, Nokia, iPhone) have their own list of features along with basic functionality of dialing, receiving a call and messaging.
 

Objects

 
Any real-world entity which can have some characteristics of which can perform some tasks is called an Object. This object is also called an instance i.e. a copy of an entity in a programming language. If we consider the above example, a mobile manufacturing company can be an object. Each object can be different based on its characteristics.

Class

 
A class in OOP is a plan which describes the object. We call it a blueprint of how the object should be represented. Mainly a class would consist of a name, attributes, and operations. Considering the above example, the Mobile can be a class, which has some attributes like Profile Type, IMEI Number, Processor, and some more. It can have operations like Dial, Receive, and SendMessage.
 

Abstraction

 
Abstraction allows us to expose limited data and functionality of objects publicly and hide the actual implementation. It is the most important pillar in OOPS. In our example of Mobile class and objects like Nokia, Samsung, iPhone.
 
Some features of mobiles,
  1. Dialing a number call some method internally which concatenates the numbers and displays it on screen but what is it doing we don’t know.
  2. Clicking on the green button actually send signals to the calling person's mobile but we are unaware of how it is doing.
This is called abstraction. In classes, we can create methods that can be called and used by the users of the class but users will have no idea what these methods do internally. 

Encapsulation







Encapsulation is defined as the process of enclosing one or more details from the outside world through access rights. It says how much access should be given to particular details. Both Abstraction & Encapsulation works hand in hand because Abstraction says what details to be made visible and Encapsulation provides the level of access right to that visible details. i.e. – It implements the desired level of abstraction.
 
Talking about Bluetooth which we usually have on our mobile. When we switch on Bluetooth, I am able to connect to other mobile or Bluetooth-enabled devices but I'm not able to access the other mobile features like dialing a number, accessing inbox, etc. This is because the Bluetooth feature is given some level of abstraction.
 
Another point is when mobile A is connected with mobile B via Bluetooth whereas mobile B is already connected to mobile C then A is not allowed to connect C via B. This is because of accessibility restrictions.
 

Polymorphism

 
Polymorphism can be defined as the ability to use the same name for doing different things. More precisely we say it as 'many forms of a single entity. This plays a vital role in the concept of OOPS.
 
Let's say Samsung mobile has a 5MP camera available i.e. – it is having the functionality of CameraClick(). Now the same mobile is having Panorama mode available in-camera, so functionality would be the same but with mode. This type is said to be Static polymorphism or Compile time polymorphism.

Inheritance



Inheritance is the ability to extend the functionality from the base entity to a new entity belonging to the same group. This will help us to reuse the functionality which is already defined before and extend into a new entity. 
 
Considering the example, the above figure 1.1 itself shows what is inheritance. Basic Mobile functionality is to send a message, dial, and receive a call. So the brands of mobile is using this basic functionality by extending the mobile class functionality and adding their own new features to their respective brand.
 
There are mainly 4 types of inheritance,
  1. Single level inheritance
  2. Multi-level inheritance
  3. Hierarchical inheritance
  4. Hybrid inheritance
  5. Multiple inheritances
Single level inheritance
 
In Single level inheritance, there is a single base class & a single derived class i.e. - A base mobile feature is extended by the Samsung brand.
 
Single level inheritance 

Multilevel inheritance
 
In Multilevel inheritance, there is more than one single level of derivation. i.e. - After base features are extended by the Samsung brand. Now Samsung brand has manufactured its new model with newly added features or advanced OS like Android OS, v4.4.2 (KitKat). From generalization, getting into more specification.
 
Multilevel inheritance 
 
Hierarchal inheritance
 
In this type of inheritance, multiple derived class would be extended from a base class, it's similar to single level inheritance but this time along with Samsung, Nokia is also taking part in inheritance.
 
Hierarchal inheritance 
 
Hybrid inheritance
 
Single, Multilevel, & hierarchal inheritance all together construct a hybrid inheritance.
 
Hybrid inheritance

Interface

 
Multiple inheritances where the derived class will extend from multiple base classes.
 
Samsung will use the function of multiple Phones (Mobile & Telephone). This would create confusion for the complier to understand which function to be called when an event in mobile is triggered like Dial () where Dial is available in both the Phone i.e. - (Mobile & Telephone). To avoid this confusion C# came with the concept of interface which is different from multiple inheritances actually.
 
If we take an interface it is similar to a class but without implementation & only declaration of properties, methods, delegates & events. The interface actually enforces the class to have a standard contract to provide all implementation to the interface members. Then what’s is the use of interface when they do not have any implementation? The answer is, they are helpful for having readymade contracts, only we need to implement functionality over this contract.
 
I mean to say, Dial would remain Dial-in case of Mobile or Telephone. It won't be fair if we give a different name when its task is to Call the person.
 
The interface is defined with the keyword 'interface'.All properties & methods within the interface should be implemented if it is been used. That's the rule of the interface.
 
Interface





Thank You For Your Visit

 






























Post a Comment

3Comments
Post a Comment
To Top