Real Life Example Of OOPS

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:
  • They take Shaadu (Fire Clay / dry mud powder), water, knot, color, brush, etc.

    Shaadu

  • First they take Shaadu in a vessel and start mixing water. Keep in mind the mixture can get very thick making your fingers ache as you try to take off the clay sticking on them.

    making your fingers

  • After this mixture is ready, give them a shape of Ganesh statue.

    shape of Ganesh statue

  • Start off with creating a base structure of the idol / Ganesh statue:

    Start off with creating a base structure

  • Leave the idol / statue to dry naturally for 2 days at least, after 2 days if statue is completely dry then start giving color to them.

    color

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.

  • Firstly, they took Shaadu in a vessel and started mixing water, after they took knot, mixed it in that pot & added this knot to mold.

    vessel and start mixing water

This mold is like a predefined class.

Class

  • Class is a blue print of newly created things.

  • Class contains characteristics (class members) and behaviors (function applied on these characteristics).

  • It is a blue print such as if we bought a land / space & would like to build a beautiful house on that land then we first draw a sketch.

  • In our programming language before going to create any application first we decide which classes we are going to create.
    1. class Ganesh_Statue {  
    2.     // class members declerations  
    3.     public string shaadu;  
    4.     public string knot;  
    5.     public string water, pot;  
    6.     public string One_Tusk, Big_Head;  
    7.     public string chain, ring;  
    8.     public string mouse_color, Eye_Color, Skin_Color;  
    9.     public void Do_Painting() {}  
    10. }  

Object

  • Objects are instances of class.

  • Whenever they create first Ganesh statue from this mold, it creates first object & it will look like the following:

    create first ganesh statue

  • Whenever they create multiple Ganesh statues (multiple objects) by using mold and added other parts like hand, legs etc, it looks like the following:

    Ganesh

Abstraction

  • Abstraction means hiding the implementation details.

  • This family only knows which type of material they have used to create a statue.

  • This family uses knot, shaadu, water, etc to make the statue and this information is hidden from the users (who can buy this Ganesh statues).

    If we ask anybody that what we can understand from the above image, then he / she will say that these are all stones.

Constructor

  • Constructor is a method that is called when instance of an object is created.

  • They have the same name as a class.

  • In our example default constructor is to be set with values such as eye color, skin color and mouse color.

  • After calling the default constructor Ganesh statue will look like the following:

    default constructor Ganesh statue
    1. //constructor  
    2. public Ganesh_Statue() {  
    3.     Console.WriteLine("**** Dafult Constructor in base class starts from here ****");  
    4.     Eye_Color = "Black";  
    5.     Skin_Color = "Redish_White";  
    6.     mouse_color = "Black";  
    7.     Console.WriteLine("Default values of Eye color: " + Eye_Color);  
    8.     Console.WriteLine("Default values of skin color: " + Skin_Color);  
    9.     Console.WriteLine("Default values of mouse colo: " + mouse_color);  
    10.     Console.WriteLine("____________________________________________________________________________");  
    11.     Console.WriteLine();  
    12. }  
    A. Parameterized Constructor

    1. A constructor with at least one parameter is called parameterized constructor.

    2. In our example suppose a wealthy businessman came to this family & gave some parameter like he wants to add his own jewellery to statue like chain & ring.
      1. // parameterized constructor  
      2. public Ganesh_Statue(string x, string y) {  
      3.     chain = x;  
      4.     ring = y;  
      5.     Console.WriteLine("____________________________________________________________________________");  
      6.     Console.WriteLine("**** Parameterized Constructor of base class starts from here ****");  
      7.     Console.WriteLine("chain color: " + x + " ring color: " + y);  
      8.     Console.WriteLine("____________________________________________________________________________");  
      9.     Console.WriteLine();  
      10. }  

Encapsulation

  • Encapsulation means biding data & function together.

  • For example, we visit the Doctor or Medical store and take medicine(capsule / tablet), then the plastic cover is used to bind with inner powder which helps us to recover.

Polymorphism

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

  • Polymorphism is the way in which we can define a multiple function in a single name. i.e single name & multiple meaning.

  • Polymorphism means assigning a single name but there can be multiple behaviors. Means name of the function is same but its definitions are different.

  • Polymorphism means single name & multiple meaning & whenever we try to access that particular name there must be a binding between that call & between that function, so there are two types of binding to call that function.

    1. Compile time binding.
    2. Run time binding

    1. Compile Time Polymorphism

    In case of this Polymorphism, function call & its definition bind at compile time.

    There are two ways to achieve this Polymorphism.

    A. Function Overloading

    In this example they (that family) hire some painter who knows only painting. If this family provides different types of statues to this painter then he has to do the work. In this way we can achieve overloading.

    Function Overloading

    1. // function overloading without parameter  
    2. public void Do_Painting() {  
    3.     Console.WriteLine("**** You are in Function Overloading Without Parameter ****");  
    4.     Console.WriteLine("____________________________________________________________________________");  
    5.     Console.WriteLine();  
    6. }  
    7.   
    8. Passing different parameters like we would like to do some painting of statue of shivaji, different solders of forts in Diwali & Durga statue etc.  
    9.   
    10. // function overloading with parameter  
    11. public void Do_Painting(string Statue_Name) {  
    12.     Console.WriteLine("**** You are in Function Overloading With Parameter ****");  
    13.     Console.WriteLine("Statue Name is: " + Statue_Name);  
    14.     Console.WriteLine("____________________________________________________________________________");  
    15.     Console.WriteLine();  
    16. }  
    Durga statue

    B. Operator Overloading

    In this example first brush is used to give a color and after finishing color of whole statue, this brush gets cleaned by using water & used for removing shadu from small surface area like between finger, below eye, between legs, corner of legs / hands, etc.

    In this case brush is used as an operator.

    brush
    1. public static void operator brush(string paint)  
    2. {  
    3.    // give color to statue using brush  
    4. }  
    5. public static void operator brush(string paint)  
    6. {  
    7.    // remove shadu from small surface area like between finger, below of eye, between legs, corner of legs / hands  
    8. }  
    Now output of our program is (refer code for more info)

    output of our program

     

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


Similar Articles