Object Oriented Programming With A Real-World Scenario

In interviews, young programmer 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 real world & programming world.
Object Oriented Programming 
We write programs to solve our problems and get our work done.
Object Oriented Programming is considered as 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 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 now a days thousands of new features and models were added and the features and number of models are still growing.
Mobile Brand 
In above diagram, each brand (Samsung, Nokia, IPhone) have their own list of features along with basic functionality of dialing, receiving a call and messaging.


Any real world entity which can have some characteristics or which can perform some tasks is called as Object. This object is also called an instance i.e. a copy of entity in programming language. If we consider the above example, a mobile manufacturing company can be an object. Each object can be different based on their characteristics. For example, here are two objects.
  1. Mobile mbl1 = new Mobile ();  
  2. Mobile mbl2 = new Mobile ();  


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.
There are some OOPS principle that need to be satisfied while creating a class. This principle is called as SOLID where each letter has some specification. I won't be going into this points deeper. A single line of each explanation may clear you with some points. 
  1. SRP (The Single Responsibility Principle) - A class should have one, and only one responsibility
  2. OCP (The Open Closed Principle) - You should be able to extend a classes behavior, without modifying it. (Inheritance)
  3. LSP (The Liskov Substitution Principle) - Derived classes must be substitutable for their base classes. (Polymorphism)
  4. ISP (The Interface Segregation Principle) -Make fine chopped interface instead of huge interface as client cannot be forced to implement an interface which they don't use.
  5. DIP (The Dependency Inversion Principle) - Depend on abstractions, not on concretions. (Abstraction)
If you want to learn more about SOLID principles, check out SOLID Architectural Pattern with Real World Example.  
Now, let's take a look at our class. A typical class based on the above discussion looks like the following in Visual Studio: 
In C# programming language, a class has members. These members are called properties, methods, fields, constructors, destructors, events, and so on. 
In code, the Mobile class looks like the following: 
  1. public class Mobile  
  2. {  
  3.      private string IEMICode { getset; }  
  4.      public string SIMCard { getset; }  
  5.      public string Processor { get; }  
  6.      public int InternalMemory { get; }  
  7.      public bool IsSingleSIM { getset; }  
  9.      public void GetIEMICode()  
  10.      {  
  11.          Console.WriteLine("IEMI Code - IEDF34343435235");  
  12.      }  
  14.      public void Dial()  
  15.      {  
  16.          Console.WriteLine("Dial a number");  
  17.      }  
  18.      public void Receive()  
  19.      {  
  20.          Console.WriteLine("Receive a call");  
  21.      }  
  22.      public virtual void SendMessage()  
  23.      {  
  24.          Console.WriteLine("Message Sent");  
  25.      }  
  26.  }  


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 concatenate the numbers and displays it on screen but what is it doing we don’t know.
  2. Clicking on green button actual send signals to 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. 
  1. public void Dial()  
  2. {  
  3.     //Write the logic  
  4.     Console.WriteLine("Dial a number");  
  5. }  


Encapsulation is defined as the process of enclosing one or more details from outside world through access right. 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 it in our mobile. When we switch on a Bluetooth, I am able to connect to another 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, 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 restriction.
In C#, a class has access modifiers such as public, private, protected, and internal. These access modifiers allows properties and methods to be exposed or restricted to the outside world.
  1. private string IMEICode = "76567556757656";  


Polymorphism can be defined as the ability of using the same name for doing different things. More precisely we say it as 'many forms of single entity'. This play a vital role in the concept of OOPS.
Let's say Samsung mobile has a 5MP camera available i.e. – it is having a functionality of CameraClick(). Now same mobile is having Panorama mode available in camera, so functionality would be same but with mode. This type is said to be Static polymorphism or Compile time polymorphism. See the example below:
  1. public class Samsumg : Mobile  
  2. {  
  3.     public void GetWIFIConnection()  
  4.     {  
  5.         Console.WriteLine("WIFI connected");  
  6.     }  
  8.     //This is one mwthod which shows camera functionality  
  9.     public void CameraClick()  
  10.     {  
  11.         Console.WriteLine("Camera clicked");  
  12.     }  
  14.     //This is one overloaded method which shows camera functionality as well but with its camera's different mode(panaroma)  
  15.     public void CameraClick(string CameraMode)  
  16.     {  
  17.         Console.WriteLine("Camera clicked in " + CameraMode + " Mode");  
  18.     }  
  19. }  
Compile time polymorphism the compiler knows which overloaded method it is going to call.
Compiler checks the type and number of parameters passed to the method and decides which method to call and it will give an error if there are no methods that matches the method signature of the method that is called at compile time.
Another point where in SendMessage was intended to send message to single person at a time but suppose Nokia had given provision for sending message to a group at once. i.e. - Overriding the functionality to send message to a group. This type is called Dynamic polymorphism or Runtime polymorphism.
For overriding you need to set the method, which can be overridden to virtual & its new implementation should be decorated with override keyword.
  1. public class Nokia : Mobile  
  2. {  
  3.      public void GetBlueToothConnection()  
  4.      {  
  5.          Console.WriteLine("Bluetooth connected");  
  6.      }  
  8.      //New implementation for this method which was available in Mobile Class  
  9.      //This is runtime polymorphism  
  10.      public override void SendMessage()  
  11.      {  
  12.          Console.WriteLine("Message Sent to a group");  
  13.      }  
  14. }  
By runtime polymorphism, we can point to any derived class from the object of the base class at runtime that shows the ability of runtime binding.


Inheritance is the ability to extend the functionality from base entity in new entity belonging to 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 inheritance
Single level inheritance
In Single level inheritance, there is single base class & a single derived class i.e. - A base mobile features is extended by 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 Samsung brand. Now Samsung brand has manufactured its new model with new 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 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 
  1. public class Mobile  
  2. {  
  3.        //Properties  
  4.        //Methods  
  5. }  
  7. public class Samsumg : Mobile  
  8. {  
  9.        //Properties  
  10.        //Methods  
  11. }  
  13. public class Nokia : Mobile  
  14. {  
  15.     //Properties  
  16.    //Methods  
  17. }  


Multiple inheritance where derived class will extend from multiple base classes.
Samsung will use the function of multiple Phone (Mobile & Telephone). This would create a confusion for complier to understand which function to be called when any 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 inheritance actually.
If we take an interface it is similar to a class but without implementation & only declaration of properties, methods, delegates & events. 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? 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 different name when its task is to Call the person.
Interface is defined with the keyword 'interface' .All properties & methods with in the interface should be implemented if it is been used. That's the rule of interface.
  1. interface IMobile  
  2. {  
  3.    Void Dial();  
  4. }  
  6. interface ITelephone  
  7. {  
  8.    void Dial();  
  10. }  
  12. public class Mobile : IMobile, ITelephone  
  14. {  
  15.     public void Dial()  
  16.     {  
  17.             Console.WriteLine("Dial a number");  
  18.     }     
  19. }  


Following the above principle and keeping in mind the four pillars of OOPS will lead you develop a good program and connecting it with the real world. I hope you like this article. Don't forget to share your comment whether it's good or bad. Sharing is valuable no matter what.
Download the file for same code.