Introduction
We are going to see here some real life examples of Object Oriented Programming (OOPS).
The following are the principles of OOPS:
- Class
- Object
- Constructor (Note: This is not actual principal)
- Abstraction
- Encapsulation
- Polymorphism
- Inheritance
- 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:
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.
- 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.
- After this mixture is ready, give them a shape of Ganesh statue.
- Start off with creating a base structure of the idol / Ganesh statue:
- 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.
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.
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.
- class Ganesh_Statue {
-
- public string shaadu;
- public string knot;
- public string water, pot;
- public string One_Tusk, Big_Head;
- public string chain, ring;
- public string mouse_color, Eye_Color, Skin_Color;
- public void Do_Painting() {}
- }
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:
- Whenever they create multiple Ganesh statues (multiple objects) by using mold and added other parts like hand, legs etc, it looks like the following:
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:
-
- public Ganesh_Statue() {
- Console.WriteLine("**** Dafult Constructor in base class starts from here ****");
- Eye_Color = "Black";
- Skin_Color = "Redish_White";
- mouse_color = "Black";
- Console.WriteLine("Default values of Eye color: " + Eye_Color);
- Console.WriteLine("Default values of skin color: " + Skin_Color);
- Console.WriteLine("Default values of mouse colo: " + mouse_color);
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine();
- }
A. Parameterized Constructor
- A constructor with at least one parameter is called parameterized constructor.
- 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.
-
- public Ganesh_Statue(string x, string y) {
- chain = x;
- ring = y;
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine("**** Parameterized Constructor of base class starts from here ****");
- Console.WriteLine("chain color: " + x + " ring color: " + y);
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine();
- }
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.
- Compile time binding.
- 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.
-
- public void Do_Painting() {
- Console.WriteLine("**** You are in Function Overloading Without Parameter ****");
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine();
- }
-
- Passing different parameters like we would like to do some painting of statue of shivaji, different solders of forts in Diwali & Durga statue etc.
-
-
- public void Do_Painting(string Statue_Name) {
- Console.WriteLine("**** You are in Function Overloading With Parameter ****");
- Console.WriteLine("Statue Name is: " + Statue_Name);
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine();
- }
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.
- public static void operator brush(string paint)
- {
-
- }
- public static void operator brush(string paint)
- {
-
- }
Now output of our program is (refer code for more info)
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:
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
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace Real_Life_Example_OOPS {
- class Ganesh_Statue {
-
- public string shaadu;
- public string knot;
- public string water, pot;
- public string One_Tusk, Big_Head;
- public string chain, ring;
- public string mouse_color, Eye_Color, Skin_Color;
-
-
-
- public Ganesh_Statue() {
- Console.WriteLine();
- Console.WriteLine("**** Dafult Constructor in base class starts from here ****");
- Eye_Color = "Black";
- Skin_Color = "Redish_White";
- mouse_color = "Black";
- Console.WriteLine("Default values of Eye color: " + Eye_Color);
- Console.WriteLine("Default values of skin color: " + Skin_Color);
- Console.WriteLine("Default values of mouse colo: " + mouse_color);
-
- Console.WriteLine();
- }
-
-
- public Ganesh_Statue(string x, string y) {
- chain = x;
- ring = y;
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine("**** Parameterized Constructor of base class starts from here ****");
- Console.WriteLine("chain color: " + x + " ring color: " + y);
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine();
- }
-
-
- public void Do_Painting() {
- Console.WriteLine("**** You are in Function Overloading Without Parameter ****");
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine();
- }
-
-
- public void Do_Painting(string Statue_Name) {
- Console.WriteLine("**** You are in Function Overloading With 1 Parameter ****");
- Console.WriteLine("Statue Name is: " + Statue_Name);
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine();
- }
-
-
-
-
-
-
-
-
-
-
-
- public void Do_Painting(string Statue_Name, string statue_2) {
- Console.WriteLine("**** You are in Function Overloading With 2 Parameter ****");
- Console.WriteLine("Statue 1st is: " + Statue_Name + "Statue 2nd is: " + statue_2);
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine();
- }
-
- }
- class Derived: Ganesh_Statue {
- public string sofa_color;
- void add_functionality() {
-
- sofa_color = "blue";
- Console.WriteLine("**** Here we are going to achieve Inheritance ****");
- Console.WriteLine("Eye color: " + Eye_Color + " skin color: " + Skin_Color);
- Console.WriteLine("Added sofa color: " + sofa_color);
- Console.WriteLine("____________________________________________________________________________");
- Console.WriteLine();
- }
- static void Main(string[] arg) {
- Ganesh_Statue obj1 = new Ganesh_Statue();
- Ganesh_Statue obj = new Ganesh_Statue("silver", "diamond");
- obj.Do_Painting();
- obj.Do_Painting("soldiers in diwali festival");
- obj.Do_Painting("animal", "shivaji statue");
- Derived obj2 = new Derived();
- obj2.add_functionality();
- Console.ReadKey();
- }
-
- }
- }
Now the output of our program is:
Destructor
Now conclude Shri Ganesha Puja with Visarjan after 7 days or 10 days. This is called
Destructor of an object.
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.