There are loads of articles on this topic but still there are not many which give short answer to the question "what is OOPs" from an interview perspective (very much needed in these troubled times :). So I thought to come up with one that does just that. If you have your interview tomorrow then you may want to skip to the end and just read the summary section. But if you have 5-10 minutes then I'll encourage you to go through this blog entry from start to end.
Below is basic known definition that majority of people will give if asked what is OOPs:
OOPS is a programming paradigm that keeps objects and methods together to create applications. In this paradigm we try to find entities that can store data, have some behavior and that can interact with one another. It involves several concepts and features like Inheritance, Polymorphism, Abstraction, and Encapsulation.
"this" pointer
Now consider green color highlighted first line of above definition and think what is that keeps objects(data) and methods(operations) together, it's "this" pointer. "this" pointer points to object(data) on which method(operation) is performed and that is fundamental thought of Object Oriented Programming. It enables us to avoid global variables and functions and ensures that operations become quality of data structure rather than a standalone operation. This in turn gives rise to approach mentioned in yellow highlighted second line of definition. You have to think about entities or actors in your project, data they are gonna hold, operations they can perform and relationship with other such entities.
Dynamic dispatch
"dynamic dispatch" is second foundation concept of OO. It is the process of selecting an implementation of a polymorphic operation(method or function) to call at run time. The purpose of dynamic dispatch is to defer the selection of an appropriate implementation until the run time type of a parameter is known (
this line I took from Wikipedia article for dynamic dispatch). This process works by coordination of object, object's type and runtime environment. Here Object's duty is to carry data about it's type, Type's duty is to keep track of its virtual function table and runtime's duty is to find concrete function address.
Now coming to blue highlighted third line of our definition, we have OOPs principals, these are consequences of foundation. There are host of fabulous articles on OOPs principles, so I will not blabber around unnecessarily on those. For the sake of completeness of this article, below are short notes to get you going on these concepts:
Abstraction and Encapsulation
Abstraction and encapsulation are complementary concepts. Abstraction focuses on the observable behavior of an object while encapsulation focuses upon the implementation that gives rise to this behavior. Abstraction has to do with separating interface from implementation while encapsulation has to do with disallowing access to or knowledge of internal structures of an implementation.
e.g, You have to create a FlightBooking system with create and cancel operations. Now your Flight booking class shall expose two methods "Create" and "Cancel" for outside world via an interface or whatever but internally there will be multiple operations like communicate with airline, take money from bank, etc. Here abstraction is the interface that you created for world to coordinate with you and Encapsulation is mechanism like making methods for internal operations private.
Inheritance
It is extending from something else, acquiring properties from a base class. E.g., a "Flight booking" extends from "Travel Booking" which in turn extends "Booking".
e.g, FlightBooking and HotelBooking classes extending from Booking class which has code for common operations like saving passenger details.
Polymorphism
It is the phenomenon wherein somewhat interchangeable objects each expose an operation of the same name but possibly differing in behavior. It allows the expression of some sort of contract, with potentially many types implementing that contract in different ways, each according to their own purpose. Both overloading(compile time polymorphism) and overriding(runtime polymorphism) are used to achieve this same concept.
e.g, FlightBooking having two methods for cancel operation, one taking no parameters and cancels whole booking while other taking passenger object so as to cancel for that passenger only.
Summary
OOPs is programming paradigm that is based on two fundamental concepts:
- "this" pointer: It points to object on which function operates and results in two important properties of OOPs
- No more global functions
- Operations become quality of data structure
- "dynamic dispatch": It is the process of selecting which implementation of a polymorphic operation (method or function) to call at run time.
- Object’s duty
- To carry data about its type
- Type’s duty
- To keep track of its virtual function table
- To override some functions from its base type
- Runtime’s duty
- Find concrete function address
OOPs principals (
Abstraction, Encapsulation, Inheritance and Polymorphism) are consequences of foundation concepts.