Basics Concepts in OOP
One of the major drawbacks of procedural programming is that it's hard to model Real world problems with it as it is more action oriented.
In object oriented programming we divide problem into a number of entities. These entities are called objects. An object have its own data and methods(or functions) which operate on this data. The data is hidden i.e. it cannot be access by functions outside of the object. OOP allows us to model real life problems in our program in a much easier way and makes the program more secure, extensible as compared to procedural approach.
Basic concepts of object oriented programming
There are some basic concepts in object oriented programming which must be understood before learning any object oriented programming language. These are,
- Objects and classes
- Data encapsulation
- Data abstraction
- Inheritance
- Polymorphism
- Dynamic binding
- Message passing
So let's discuss these one by one,
Objects and classes
As explained earlier, objects allows us to model real life problems easily. For example if you are developing a program for handling banking tasks, then it may have classes like bank account customers etc and may have instance of customer class like Jhon doe , or let's say we are working on a racing game then it may have objects like cars bikes buildings etc.An object can be made a user defined data type with the help of class.So, an object can also be said as a variable of type class.
In fact we define a class before we create object of that class. Class behaves as a template for creating new objects of its type. A Classic example of classes and objects is that mango apple banana are members of class fruit. Similarly in our example of a racing game, car and bike can be a member of class vehicle.
Classes behaves just like built in data types in programming language.
Data encapsulation
To Encapsulate means to wrap up something in a single unit. In object oriented programming we wrap up functions and their associated data in a single unit(Class). .This is known as data encapsulation. The data(Methods and variables) is divided into two sections, public and private. the data in public section can be accessed outside the object.But the data in private section is not accessible outside of the class i.e. its hidden from outside world. This is called Data hiding. This makes program more organized and more secure.
Data abstraction
Representing essential features without including the background details is known as abstraction. Classes in object oriented programming use the concept of abstraction as they are defined as a list of abstract attributes and functions to operate on these are attributes. Because of this classes are also called as abstract data type.
Inheritance
As the name suggests, its just like we all inherit something from our parents like facial features physical appearance and what not.Idea is that a class can inherit properties of another class. The class from which properties are inherited is known as the base class and the one which inherits is known as derived class. This is really a very powerful concept in object oriented programming. It not only helps us to model Real world problems efficiently but also reduces redundancy in our code. Inheritance is for code re usability.
Polymorphism
In OOP objects of same class can behave differently dynamically. It is called polymorphism.Poly means "many" and morphism means "form".
It means objects can take different forms under different conditions.
For example, consider a class vehicle with method accelerate() and its objects such as bike, truck and car. When accelarate() of bike will be called it will work differently than it will work if called for truck. At run time we the code matching the call will be called. Here, at complie time we didn't know which code to call, this is done at run time and it is called binding(call to code).As it is done dynamically at runtime hence it is called Dynamic binding. And the binding which is done at the compile time is known as Static binding and it is done by compiler.
Message Passing
In an OOP objects can communicate with each other. This communication is done using technique called message passing. In Message Passing we basically pass message to an object.
You explained this in such a simple manner and I really appreciate that! I can imagine this being a useful resource for students or people looking to gain a beginner's understanding of these concepts.
ReplyDelete