Introduction

We are going to see here some real life examples of Object Oriented Programming (OOPS).

The following are the principles of OOPS:

  1. Class
  2. Object
  3. Constructor (Note: This is not actual principal)
  4. Abstraction
  5. Encapsulation
  6. Polymorphism
  7. Inheritance
  8. Destructor (Note: This is not actual principal)

Description

Before proceeding further let’s look into background.Before OOPS concept came into picture (market) there was "C Language" designed & developed by Dennis Ritchie in 1972 at AT & T Laboratory.

C language is a general purpose, block structured, case sensitive & procedure oriented language.
Program execution always goes from top to bottom.

Let’s see how a C program gets compiled & run.

CLR like the following figure:

CLR

Example

This example is divided into two parts; firstly, Procedure Oriented approach and secondly Object oriented approach.

There is one poor family in Maharashtra. They have their own business to pick up fish from sea & sell that in the local market. In that family there were members including husband, wife & children. Everyday they do the same thing for their business. After some years they did not earned a good amount of profit from their business because daily expenses were bigger than what they earned from the business.

So husband & wife decided to do some other business. So they visited on of their relatives and ask for some suggestions. They got a suggestion to make Ganesh statues to sell them. This was suggested because it is one of the best business since Ganesh Chaturthi is an important festival and celebrated across Indian homes.

In this business they don’t need to invest extra money. So the family stated making Ganesh statues.

They follow the procedural approach like C program approach:

For doing the above procedure they need extra time & days because firstly they have to give it a proper shape, statue should need to dry completely, after that give proper color to this statue. If sometimes statue gets cracked or shape didn't came out well, then they have do repeat the whole procedure from first to last. So it is very time consuming.

In this procedure suppose for completing 1 Ganesh statue it takes 5 days i.e approximately each month they completed 6 Ganesh statue (every year 72).

For first year they sold 72 statues. After this 72 statue reached in 72 homes, relatives & neighbours loved the statue. So for next year this family got an advance order of 2000 Ganesh statue which are in different shapes like 1 feet, 2 feet, 5 feet, 10 feet & 20 feet.

The family member again visited their relative to resolve these relevant questions i.e How can they complete 2000 Ganesh statues in one year? So they gave suggestion to use mold (meaning - a hollow container used to give shape to molten or hot liquid material when it cools and hardens) to give proper shapes to Ganesh statues.

Now the family used these different molds like 1 feet, 2 feet, 5 feet, 10 feet & 20 feet. Following this, they can complete 100 Ganesh statue per day.

Here they followed the procedure which is Object Oriented Programming Approach.

This mold is like a predefined class.

Class

Object

Abstraction

Constructor

Encapsulation

Polymorphism

Now this family has to decide about some people who have domain specific knowledge like painting.

Inheritance

Now suppose the wealthy person comes at this family’s house. He shows some photos of Lalbag Raja (Mumbai) to this family & now he need the same Ganesh statue like the following:

Ganesh statue

So as shown in the above image this family decides to add mold with seat on a gold-plated stool decked with ornaments (i.e. gold sofa).

In this situation, if we see our class is already ready i.e. base class from which we created Ganesh statues, we need to add a new functionality i.e Inheritance.

When people buy the Ganesh statue, they keep it at their home. Puja should begin with invocation of Lord Ganesha, one should chant Mantra in front of the Murti, by showing Avahan Mudra (Avahan Mudra is formed by joining both palms and folding both thumbs inwards).

After Lord Ganesha has been invoked and got installed, take five flowers in Anjali (by joining palm of both hands).

Program

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Real_Life_Example_OOPS {
  7. class Ganesh_Statue {
  8. // class members declerations
  9. public string shaadu;
  10. public string knot;
  11. public string water, pot;
  12. public string One_Tusk, Big_Head;
  13. public string chain, ring;
  14. public string mouse_color, Eye_Color, Skin_Color;
  15. //constructor
  16. public Ganesh_Statue() {
  17. Console.WriteLine();
  18. Console.WriteLine("**** Dafult Constructor in base class starts from here ****");
  19. Eye_Color = "Black";
  20. Skin_Color = "Redish_White";
  21. mouse_color = "Black";
  22. Console.WriteLine("Default values of Eye color: " + Eye_Color);
  23. Console.WriteLine("Default values of skin color: " + Skin_Color);
  24. Console.WriteLine("Default values of mouse colo: " + mouse_color);
  25. Console.WriteLine();
  26. }
  27. // parameterized constructor
  28. public Ganesh_Statue(string x, string y) {
  29. chain = x;
  30. ring = y;
  31. Console.WriteLine("____________________________________________________________________________");
  32. Console.WriteLine("**** Parameterized Constructor of base class starts from here ****");
  33. Console.WriteLine("chain color: " + x + " ring color: " + y);
  34. Console.WriteLine("____________________________________________________________________________");
  35. Console.WriteLine();
  36. }
  37. // function overloading without parameter
  38. public void Do_Painting() {
  39. Console.WriteLine("**** You are in Function Overloading Without Parameter ****");
  40. Console.WriteLine("____________________________________________________________________________");
  41. Console.WriteLine();
  42. }
  43. // function overloading with parameter
  44. public void Do_Painting(string Statue_Name) {
  45. Console.WriteLine("**** You are in Function Overloading With 1 Parameter ****");
  46. Console.WriteLine("Statue Name is: " + Statue_Name);
  47. Console.WriteLine("____________________________________________________________________________");
  48. Console.WriteLine();
  49. }
  50. // operator overloading
  51. //public static void operator brush(string paint)
  52. //{
  53. // // give color to statue
  54. //}
  55. // public static void operator brush(string paint)
  56. //{
  57. // // remove shadu from small surface area like between finger, below of eye, between legs, corner of legs / hands
  58. //}
  59. public void Do_Painting(string Statue_Name, string statue_2) {
  60. Console.WriteLine("**** You are in Function Overloading With 2 Parameter ****");
  61. Console.WriteLine("Statue 1st is: " + Statue_Name + "Statue 2nd is: " + statue_2);
  62. Console.WriteLine("____________________________________________________________________________");
  63. Console.WriteLine();
  64. }
  65. }
  66. class Derived: Ganesh_Statue {
  67. public string sofa_color;
  68. void add_functionality() {
  69. sofa_color = "blue";
  70. Console.WriteLine("**** Here we are going to achieve Inheritance ****");
  71. Console.WriteLine("Eye color: " + Eye_Color + " skin color: " + Skin_Color);
  72. Console.WriteLine("Added sofa color: " + sofa_color);
  73. Console.WriteLine("____________________________________________________________________________");
  74. Console.WriteLine();
  75. }
  76. static void Main(string[] arg) {
  77. Ganesh_Statue obj1 = new Ganesh_Statue();
  78. Ganesh_Statue obj = new Ganesh_Statue("silver", "diamond");
  79. obj.Do_Painting();
  80. obj.Do_Painting("soldiers in diwali festival");
  81. obj.Do_Painting("animal", "shivaji statue");
  82. Derived obj2 = new Derived();
  83. obj2.add_functionality();
  84. Console.ReadKey();
  85. }
  86. }
  87. }
Now the output of our program is:

output

Destructor

Now conclude Shri Ganesha Puja with Visarjan after 7 days or 10 days. This is called Destructor of an object.

Destructor

Summary

This article will help freshers as well as experienced candidates to crack the technical interview rounds. In many interviews interviewers ask about real life OOPS example.

Hope you enjoyed this complete article on Real Life Examples. Don’t forget to comment on the article.