Definition of ( OOP ) – Object Oriented Programming
OOP is an approach that provides way of modulating programs by creating partitioned memory area for both data and functions that can be used as templates for creating copies of such modules on demand.
Some of the features of OOP Paradigm are:
- Emphasis is on data rather than procedure.
- Programs are divided into what are known as Objects.
- Data structures are designed such that they characterize the objects.
- Methods that operate on the data are tied together in the data structure.
- Data is hidden and can not be accessed by external functions.
- Objects may communicate with each other through methods.
- New data and methods can be easily added whenever necessary.
- Follows bottom-up approach in program design
Basic concepts of OOP:
Classes & Objects
Data abstraction and encapsulation
Class is a blue print which is containing only list of variables and method and no memory is allocated for them. A class is a group of objects that has common properties.
Class is also can be used to achieve user defined data types.
A class in java contains:
Class and Interface
Object is a instance of class, object has state and behaviors.
An Object in java has three characteristics:
State: Represents data (value) of an object.
Behavior: Represents the behavior (functionality) of an object such as deposit, withdraw etc.
Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user.
But,it is used internally by the JVM to identify each object uniquely.
Real life example of object and class
In real world many examples of class and object
River , Country , Animal are class .
where India , Japan, USA, UK are object of country class
where Ganga, Yamuna , Narmada, Sarswati are objects of River class
cow , cat, and dog are belong to animal’s class.
Each object has state and behaviors.
For example a dog has
state:- color, name, height, age,trail
behaviors:- Running, barking, eating, and sleeping, waving .
Encapsulation in Java is a mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.
In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class. Therefore, it is also known as data hiding.
To achieve encapsulation in Java –
Declare the variables of a class as private.
Provide public setter and getter methods to modify and view the variables values.
so above all Encapsulation enables you to hide, inside the object, both the data and the methods that act on that data, therefore we can control access to the data, forcing programs to retrieve or modify data only through the object’s interface.
Abstraction is a process of hiding the implementation
details from the user, only the functionality will be provided to the user.
In other words, the user will have the information on what the object does instead of how it does it.
In Java, abstraction is achieved using Abstract classes and interfaces.
Real word example A car we operate , we Drive it but we can not knows about its mechanism system.
Difference between Encapsulation and Abstraction
Encapsulation is a process of hiding all the internal details of an object from the outside real world.
The word encapsulation, is like “enclosing” into a “capsule”.
It restricts clients from seeing its internal view where the behavior of the abstraction is implemented.
Abstraction allows us to represent complex real world in simplest manner.
It is the process of identifying the relevant qualities and behaviors an object should possess.
In other words, to represent the necessary feature without representing the background details.
- Inheritance enables you to create a class that is similar to a previously defined class, but one that still has some of its own properties.
- Consider a car-simulation program. Suppose that you have a class for a regular car, but now you want to create a car that has a high-speed passing gear. In a traditional program, you might have to modify the existing code.
- To avoid this problem, we use the object-oriented approach: Create a new class by inheritance. This new class inherits all the data and methods from the tested base class.
- By using polymorphism, you can create new objects that perform the same functions as the base object but which perform one or more of these functions in a different way.
- For example, you may have a shape object that draws a circle on the screen. By using polymorphism, you can create a shape object that draws a rectangle instead.
- You do this by creating a new version of the method that draws the shape on the screen
Advantages of OOP:
- Through inheritance, we can eliminate redundant code and extend the use of existing classes.
- The principle of data hiding helps the programmer to build secure programs that can not be attacked by code in other parts of the program.
- It is possible to have multiple objects to coexist without interference.
- It is possible to map object in problem domain to those objects in the programs.
- The data centered approach enables us to capture more details of a model in an implementable form.
- Object oriented systems can be easily upgraded from small to large systems.
- Message passing techniques for communicating between objects make the interface descriptions with external systems much simpler.
- Software complexity can be easily managed.
Applications of OOP:
- Real time systems
- Simulation and modeling
- Object oriented database
- Hypertext, hypermedia and experts
- AI and expert systems
- Neural network send parallel programming.
- Decision support and office automation systems
- CAD/CAM systems