Object oriented Programming can be seen as collection of objects coordinating with each other rather than the traditional old approach of programs being viewed as a set of tasks (subroutines) given to the computer (Procedural programming Model).
Each object is independent of other with a distinct role and responsibility consisting of actions or operators (which can be applied on that object) closely associated with each other. For example any OOP language sees human as an integration of its state (the data/variables or the characteristics that describe the human) and the behavior (the functions that human can perform like eat, sleep etc).
Essentially Data and its functionality is combined and wrapped around to form an object.
One principal benefit of OOP over the procedural approach is that OOP allows you to write modules that need not be changed when new objects are added. You just need to inherit that class and use the features provided by that object.
Each object is capable of receiving and sending messages to other objects, processing data.
Each object can be viewed with independent role and responsibility.
Most of modern programming languages now support OOP.
Basic Concepts of OOPAbstraction:Abstraction is a way to simplify complex problems.
It is a mechanism and practice to reduce and factor out details so that one can focus on few concepts at a time.
It can be achieved through composition.
It helps different objects to interact with each other.
For example, a car must have certain basic set of attributes like gearbox, steering etc. But a car is not concerned about what are the inner details of these things. All it is concerned about is that how these will work when combined together.
Encapsulation:
Concealing an object’s detail is called Encapsulation.
It is a mechanism and practice to reduce and factor out details so that one can focus on few concepts at a time.
When we do encapsulation, each class is exposed to an interface.
The basic reason for Encapsulation is to expose to the client with only that much code that is necessary for him to know. This helps in changing the core logic easily.
It can be used to hide physical storage layout for data so that if it is changed, the change is restricted to a small subset of a total program.
For example, Maruti may have a method for starting the engine but it’s not necessary that Opel must know about it. All that Opel must be concerned about is that Maruti can start the engine.
Polymorphism:
Polymorphism is the ability of an object belonging to different genre (classes/data types) respond to method calls of a method having the same name each one according to the type-specific behavior.
Polymorphism can be achieved in either of the two ways:
Overloading methods - One method signature to call different functions based on the implementation.
Overriding methods - Providing different implementation of the parent class method in the child class.
For example, accelerate() is a method of a class Car, but it has different way of implementation for each type of car. accelerate() method is overridden in each derived class to add a class specific behavior.
Properties of OOPClass:
A class defines the abstract characteristic of an object along with its common traits. For example, a class dog must contain all general characteristic that any dog does posses and the traits (the things that any dog can do).
A class must be self descriptive and must provide modularity and structure to the program.
A class is blueprint or prototype from which objects are created.
A class models the state and behavior of a real world or an abstract object, but characteristics of the class should make sense in context.
A class must be such that even a lay man who knows the business domain must come to know what the class does by looking at it. This means that a class must be self-contained and must make sense in the context.
A class contains properties and methods which are collectively called Members of the class.
For example, we can say that class Car would consist of some common features, such as color and type of windshields (characteristics), and ability to achieve maximum speed (behavior).
Object:
An object is a particular instance of a class consisting of state and behavior of the object’s class.
For example, a car has a color, but Maruti 800 has a red color. So we can say that object Maruti 800 is an instance of class car.
Method:
An object’s abilities comprise of methods.
Method serves as a primary mechanism for object-to-object communication.
A method affects only the object which calls it and operates on its internal state.
An object Maruti 800 has an ability to accelerate. So accelerate () is one of the Maruti 800’s method.
Inheritance:
Inheritance is the same as the name suggests. A sub-class may inherit attributes and methods from its parent class and may introduce some of its own.
A sub-class is more specific version of its parent class.
It is also known as generalization.
It is intended to help reuse exiting code with little or no modifications.
Inherited class (derived class), take over attributes and behavior of the pre-existing classes.
For example, consider a class car. Then a class Maruti will inherit all the attributes of the class car like fuel tank, engine etc and methods such as economy, steering etc along with adding specific properties like shape, color, seats etc.
Inheritance is an ‘is-a’ relationship. Like Maruti is a car.
There is also one more type of inheritance that is Multiple Inheritance which may not always be supported as it is trivial to implement and use. (A class when derives more than one class i.e. has more than one parent class is said to have done Multiple Inheritance).
Message Passing:
Message passing is a way by which an object sends data to other object or invokes other objects method.
In programming world it is known as Interfacing.
For example, suppose that in our example of car class, the object of fuel tank wants to supply fuel to the engine object, it calls the appropriate method of engine in order to do so.
A pure object oriented programming language is one that supports all the above mentioned concepts and properties.