SlideShare a Scribd company logo
Article By:- Durgesh Kumar Singh
(Enterpreneur, Researcher, Developer, Author, Trainer and Speaker)
--------------------------------------------------------------------------------------------------------------------------------
Object Oriented Programming With Real-
World Scenario
Normally every interviewer ask for a real world scenario explaining OOP and many of them fail to
answer. This is the reason I am writing this article. This article is mainly intended for the audience
who are knowing the Object Oriented Programming (OOP) concept theoretically but are unable
to link with real world & programming world.
We write programs to solve our problem and get our work done.
Object Oriented Programming is basically considered as design methodology for creating a non-
rigid application. In OOPS every logic is written to get our work done, but this is done based on
entity which we call it as Objects. OOP allow us to decompose our problem in to small unit of
work which are accessed via Objects. We build function around this objects. There are mainly
four pillars (features) of OOP.
If all this features fulfill our programming, then we can say it as perfect Object Oriented
Programming.
1. Abstraction
2. Encapsulation
3. Inheritance
4. Polymorphism
Let’s consider an example for explaining each pillar, which at the end will make you understand
& follow Object Oriented Programming. Before that, we need to know something
When we take a mobile 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 & models were
added & the count is still increasing.
In above diagram, each brand (Samsung, Nokia, IPhone) have their own list of features along
with basic functionality of dialing, receiving a call & messaging.
When we talk about OOP, as the word indicate it will talk about an object (a real world object)
Objects
Any real world entity which can have some characteristics or which can perform some work is
called as Object. This object is also called as an instance i.e. - a copy of entity in programming
language. If we consider the above example, a mobile manufacturing company, at a time
manufactures lacs of pieces of each model which are actually an instance. This objects are
differentiated from each other via some identity or its characteristics. This characteristics is given
some unique name.
1. Mobile mbl1 = new Mobile ();
2. Mobile mbl2 = new Mobile ();
Class
A Class is a plan which describes the object. We call it as a blue print of how the object should
be represented. Mainly a class would consist of a name, attributes & operations. Considering the
above example, A Mobile can be a class which has some attributes like Profile Type, IMEI
Number, Processor, and some more.) & operations like Dial, Receive & SendMessage.
There are some OOPS principle that need to be looked in while creating any of the 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.
SRP (The Single Responsibility Principle)
A class should have one, and only one responsibility
OCP (The Open Closed Principle)
You should be able to extend a classes behavior, without modifying it. (Inheritance)
LSP (The Liskov Substitution Principle)
Derived classes must be substitutable for their base classes. (Polymorphism)
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.
DIP (The Dependency Inversion Principle)
Depend on abstractions, not on concretions. (Abstraction)
Now this class is represented as shown below
1. public class Mobile
2. {
3. private string IEMICode { get; set; }
4. public string SIMCard { get; set; }
5. public string Processor { get; }
6. public int InternalMemory { get; }
7. public bool IsSingleSIM { get; set; }
8.
9. public void GetIEMICode()
10. {
11. Console.WriteLine("IEMI Code - IEDF34343435235");
12. }
13.
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
Abstraction says, only show relevant details and rest all hide it. This is most important pillar in
OOPS as it is providing us the technique to hide irrelevant details from User. If we consider an
example of any mobile 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 where creating method which is taking some parameter & returning
some result after some logic execution without understating what is written within the method
1. public void Dial()
2. {
3. //Write the logic
4. Console.WriteLine("Dial a number");
5. }
Encapsulation
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 & 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 the Bluetooth
I am able to connect another mobile but 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.
This is handled by access specifier like public, private, protected, and internal
1. private string IMEICode = "76567556757656";
Polymorphism
Polymorphism can be defined as the ability of doing the same operation but with different type of
input.
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 have the 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. }
7.
8. //This is one mwthod which shows camera functionality
9. public void CameraClick()
10. {
11. Console.WriteLine("Camera clicked");
12. }
13.
14. //This is one overloaded method which shows camera functionality as well but wi
th 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. }
7.
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
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 defined before.
Considering the example, the above figure 1.1 itself shows what is inheritance. Basic Mobile
functionality is to Send Message, dial & receive 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.
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.
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.
Hybrid inheritance
Single, Multilevel, & hierarchal inheritance all together construct a hybrid inheritance.
1. public class Mobile
2. {
3. //Properties
4. //Methods
5. }
6.
7. public class Samsumg : Mobile
8. {
9. //Properties
10. //Methods
11. }
12.
13. public class Nokia : Mobile
14. {
15. //Properties
16. //Methods
17. }
Interface
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. }
5.
6. interface ITelephone
7. {
8. void Dial();
9.
10. }
11.
12. public class Mobile : IMobile, ITelephone
13.
14. {
15. public void Dial()
16. {
17. Console.WriteLine("Dial a number");
18. }
19. }
Conclusion
Following the above principle & keeping in mind the four pillars would lead you develop a good
program & linking it with real world scenario will make you think more deeply. 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.
Ad

More Related Content

What's hot (20)

Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
RAGAVIC2
 
Inheritance
InheritanceInheritance
Inheritance
Sapna Sharma
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
kamal kotecha
 
Js set timeout & setinterval
Js set timeout & setintervalJs set timeout & setinterval
Js set timeout & setinterval
ARIF MAHMUD RANA
 
Java constructors
Java constructorsJava constructors
Java constructors
QUONTRASOLUTIONS
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
CPD INDIA
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
Inheritance in JAVA PPT
Inheritance  in JAVA PPTInheritance  in JAVA PPT
Inheritance in JAVA PPT
Pooja Jaiswal
 
Python Control structures
Python Control structuresPython Control structures
Python Control structures
Siddique Ibrahim
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
Indu Sharma Bhardwaj
 
class and objects
class and objectsclass and objects
class and objects
Payel Guria
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
RahulAnanda1
 
Fragment
Fragment Fragment
Fragment
nationalmobileapps
 
Coroutines in Kotlin
Coroutines in KotlinCoroutines in Kotlin
Coroutines in Kotlin
Alexey Soshin
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
KALAISELVI P
 
C# Basics
C# BasicsC# Basics
C# Basics
Sunil OS
 
An Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: ArraysAn Introduction to Programming in Java: Arrays
An Introduction to Programming in Java: Arrays
Martin Chapman
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
sai tarlekar
 

Similar to Object Oriented Programming With Real-World Scenario (20)

ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
Java oop concepts
Java oop conceptsJava oop concepts
Java oop concepts
Syeful Islam
 
NAOqi framework
NAOqi frameworkNAOqi framework
NAOqi framework
Atelier for robotics
 
Oopp Lab Work
Oopp Lab WorkOopp Lab Work
Oopp Lab Work
Heather Dionne
 
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
NicheTech Com. Solutions Pvt. Ltd.
 
CS305PC_C++_UNIT 1 notes jntuh third semester
CS305PC_C++_UNIT 1 notes jntuh third semesterCS305PC_C++_UNIT 1 notes jntuh third semester
CS305PC_C++_UNIT 1 notes jntuh third semester
VeeraswamyDasari2
 
Basics of object oriented programming
Basics of object oriented programmingBasics of object oriented programming
Basics of object oriented programming
Nitin Kumar Kashyap
 
Polymorphism.Difference between Inheritance & Polymorphism
Polymorphism.Difference between Inheritance & PolymorphismPolymorphism.Difference between Inheritance & Polymorphism
Polymorphism.Difference between Inheritance & Polymorphism
huzaifaakram12
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
Praveen M Jigajinni
 
MCA NOTES.pdf
MCA NOTES.pdfMCA NOTES.pdf
MCA NOTES.pdf
RAJASEKHARV10
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++
Mohamed Essam
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
Amresh Raj
 
M.c.a. (sem iv)- java programming
M.c.a. (sem   iv)- java programmingM.c.a. (sem   iv)- java programming
M.c.a. (sem iv)- java programming
Praveen Chowdary
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
Durgesh Singh
 
Architecture your android_application
Architecture your android_applicationArchitecture your android_application
Architecture your android_application
Mark Brady
 
OOP Lab-manual btech in cse kerala technological university
OOP Lab-manual btech in cse kerala technological universityOOP Lab-manual btech in cse kerala technological university
OOP Lab-manual btech in cse kerala technological university
seenamt1234
 
Principal of objected oriented programming
Principal of objected oriented programming Principal of objected oriented programming
Principal of objected oriented programming
Rokonuzzaman Rony
 
DDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFrameworkDDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFramework
banq jdon
 
Android - Open Source Bridge 2011
Android - Open Source Bridge 2011Android - Open Source Bridge 2011
Android - Open Source Bridge 2011
sullis
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
kristinatemen
 
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
Objective of c in IOS , iOS Live Project Training Ahmedabad, MCA Live Project...
NicheTech Com. Solutions Pvt. Ltd.
 
CS305PC_C++_UNIT 1 notes jntuh third semester
CS305PC_C++_UNIT 1 notes jntuh third semesterCS305PC_C++_UNIT 1 notes jntuh third semester
CS305PC_C++_UNIT 1 notes jntuh third semester
VeeraswamyDasari2
 
Basics of object oriented programming
Basics of object oriented programmingBasics of object oriented programming
Basics of object oriented programming
Nitin Kumar Kashyap
 
Polymorphism.Difference between Inheritance & Polymorphism
Polymorphism.Difference between Inheritance & PolymorphismPolymorphism.Difference between Inheritance & Polymorphism
Polymorphism.Difference between Inheritance & Polymorphism
huzaifaakram12
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
Ruth Marvin
 
Chapter 04 object oriented programming
Chapter 04 object oriented programmingChapter 04 object oriented programming
Chapter 04 object oriented programming
Praveen M Jigajinni
 
OOP-Advanced Programming with c++
OOP-Advanced Programming with c++OOP-Advanced Programming with c++
OOP-Advanced Programming with c++
Mohamed Essam
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
Amresh Raj
 
M.c.a. (sem iv)- java programming
M.c.a. (sem   iv)- java programmingM.c.a. (sem   iv)- java programming
M.c.a. (sem iv)- java programming
Praveen Chowdary
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
Durgesh Singh
 
Architecture your android_application
Architecture your android_applicationArchitecture your android_application
Architecture your android_application
Mark Brady
 
OOP Lab-manual btech in cse kerala technological university
OOP Lab-manual btech in cse kerala technological universityOOP Lab-manual btech in cse kerala technological university
OOP Lab-manual btech in cse kerala technological university
seenamt1234
 
Principal of objected oriented programming
Principal of objected oriented programming Principal of objected oriented programming
Principal of objected oriented programming
Rokonuzzaman Rony
 
DDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFrameworkDDD Framework for Java: JdonFramework
DDD Framework for Java: JdonFramework
banq jdon
 
Android - Open Source Bridge 2011
Android - Open Source Bridge 2011Android - Open Source Bridge 2011
Android - Open Source Bridge 2011
sullis
 
Ad

Recently uploaded (20)

Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Grade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable WorksheetGrade 2 - Mathematics - Printable Worksheet
Grade 2 - Mathematics - Printable Worksheet
Sritoma Majumder
 
Kenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 CohortKenan Fellows Participants, Projects 2025-26 Cohort
Kenan Fellows Participants, Projects 2025-26 Cohort
EducationNC
 
Link your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRMLink your Lead Opportunities into Spreadsheet using odoo CRM
Link your Lead Opportunities into Spreadsheet using odoo CRM
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
To study the nervous system of insect.pptx
To study the nervous system of insect.pptxTo study the nervous system of insect.pptx
To study the nervous system of insect.pptx
Arshad Shaikh
 
Real GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for SuccessReal GitHub Copilot Exam Dumps for Success
Real GitHub Copilot Exam Dumps for Success
Mark Soia
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
Contact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: OptometryContact Lens:::: An Overview.pptx.: Optometry
Contact Lens:::: An Overview.pptx.: Optometry
MushahidRaza8
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
"Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules""Basics of Heterocyclic Compounds and Their Naming Rules"
"Basics of Heterocyclic Compounds and Their Naming Rules"
rupalinirmalbpharm
 
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdfIntroduction-to-Communication-and-Media-Studies-1736283331.pdf
Introduction-to-Communication-and-Media-Studies-1736283331.pdf
james5028
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Ad

Object Oriented Programming With Real-World Scenario

  • 1. Article By:- Durgesh Kumar Singh (Enterpreneur, Researcher, Developer, Author, Trainer and Speaker) -------------------------------------------------------------------------------------------------------------------------------- Object Oriented Programming With Real- World Scenario Normally every interviewer ask for a real world scenario explaining OOP and many of them fail to answer. This is the reason I am writing this article. This article is mainly intended for the audience who are knowing the Object Oriented Programming (OOP) concept theoretically but are unable to link with real world & programming world. We write programs to solve our problem and get our work done. Object Oriented Programming is basically considered as design methodology for creating a non- rigid application. In OOPS every logic is written to get our work done, but this is done based on entity which we call it as Objects. OOP allow us to decompose our problem in to small unit of work which are accessed via Objects. We build function around this objects. There are mainly four pillars (features) of OOP. If all this features fulfill our programming, then we can say it as perfect Object Oriented Programming. 1. Abstraction 2. Encapsulation 3. Inheritance 4. Polymorphism Let’s consider an example for explaining each pillar, which at the end will make you understand & follow Object Oriented Programming. Before that, we need to know something When we take a mobile 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 & models were added & the count is still increasing.
  • 2. In above diagram, each brand (Samsung, Nokia, IPhone) have their own list of features along with basic functionality of dialing, receiving a call & messaging. When we talk about OOP, as the word indicate it will talk about an object (a real world object) Objects Any real world entity which can have some characteristics or which can perform some work is called as Object. This object is also called as an instance i.e. - a copy of entity in programming language. If we consider the above example, a mobile manufacturing company, at a time manufactures lacs of pieces of each model which are actually an instance. This objects are differentiated from each other via some identity or its characteristics. This characteristics is given some unique name. 1. Mobile mbl1 = new Mobile (); 2. Mobile mbl2 = new Mobile (); Class
  • 3. A Class is a plan which describes the object. We call it as a blue print of how the object should be represented. Mainly a class would consist of a name, attributes & operations. Considering the above example, A Mobile can be a class which has some attributes like Profile Type, IMEI Number, Processor, and some more.) & operations like Dial, Receive & SendMessage. There are some OOPS principle that need to be looked in while creating any of the 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. SRP (The Single Responsibility Principle) A class should have one, and only one responsibility OCP (The Open Closed Principle) You should be able to extend a classes behavior, without modifying it. (Inheritance) LSP (The Liskov Substitution Principle) Derived classes must be substitutable for their base classes. (Polymorphism) 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. DIP (The Dependency Inversion Principle) Depend on abstractions, not on concretions. (Abstraction) Now this class is represented as shown below 1. public class Mobile 2. { 3. private string IEMICode { get; set; } 4. public string SIMCard { get; set; } 5. public string Processor { get; } 6. public int InternalMemory { get; } 7. public bool IsSingleSIM { get; set; } 8. 9. public void GetIEMICode()
  • 4. 10. { 11. Console.WriteLine("IEMI Code - IEDF34343435235"); 12. } 13. 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 Abstraction says, only show relevant details and rest all hide it. This is most important pillar in OOPS as it is providing us the technique to hide irrelevant details from User. If we consider an example of any mobile 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 where creating method which is taking some parameter & returning some result after some logic execution without understating what is written within the method 1. public void Dial() 2. { 3. //Write the logic 4. Console.WriteLine("Dial a number"); 5. } Encapsulation
  • 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 & 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 the Bluetooth I am able to connect another mobile but 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. This is handled by access specifier like public, private, protected, and internal 1. private string IMEICode = "76567556757656"; Polymorphism Polymorphism can be defined as the ability of doing the same operation but with different type of input. 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 have the 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()
  • 6. 4. { 5. Console.WriteLine("WIFI connected"); 6. } 7. 8. //This is one mwthod which shows camera functionality 9. public void CameraClick() 10. { 11. Console.WriteLine("Camera clicked"); 12. } 13. 14. //This is one overloaded method which shows camera functionality as well but wi th 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. } 7. 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
  • 7. 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 defined before. Considering the example, the above figure 1.1 itself shows what is inheritance. Basic Mobile functionality is to Send Message, dial & receive 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. 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.
  • 8. 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. Hybrid inheritance Single, Multilevel, & hierarchal inheritance all together construct a hybrid inheritance.
  • 9. 1. public class Mobile 2. { 3. //Properties 4. //Methods 5. } 6. 7. public class Samsumg : Mobile 8. { 9. //Properties 10. //Methods 11. } 12. 13. public class Nokia : Mobile 14. { 15. //Properties 16. //Methods 17. } Interface 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.
  • 10. 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. } 5. 6. interface ITelephone 7. { 8. void Dial(); 9. 10. } 11. 12. public class Mobile : IMobile, ITelephone 13. 14. { 15. public void Dial() 16. { 17. Console.WriteLine("Dial a number"); 18. } 19. } Conclusion Following the above principle & keeping in mind the four pillars would lead you develop a good program & linking it with real world scenario will make you think more deeply. 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.