What is OOP (Object Oriented Programming)
OOP (object-oriented programming ) is a programming language module for software design that mainly relies on classes and objects.
In this module, the program is built around the data. In other words, A program written in a style which consists of interacting with objects is called object-oriented programing. Because of this OOP is closer to the real world. So, it is more popular than other modules.
OOP is well suited for programs that are large, complex and actively updated or maintained.
There are two main concepts in OOP,
classes
objects
Class: Human ---> Object: Man, Woman
Class: fruit ---> Object: Apple, Banana, Mango
Classes are simple reusable pieces of code that act as a blueprint in the program. ( blueprint of the object). Class is a kind of template for objects. Classes consist of a set of attributes and methods that are common to all objects.
Before creating an object we need to define a class.
Create a class in java,
class ClassName {
// attributes
// methods
}
// If we consider a CAR class,
class Car {
// attributes
private string color;
//methods
private void accelerate() {
system.out.println("moving forword.");
}
}
An object is called an instance of the class.
Create an object in java,
className object = new className();
// for Car class
Car myCar = new Car();
Car helensCar = new Car();
// access members of Car class (reffer to upper code)
myCar.color;
myCar.accelerate();
Object | Class |
An object is created many times as per requirement. | Class is declared once. |
allocates memory when it is created | Class doesn't allocate memory when it is created. |
An Object is one instance of a specific class. | A class is a template for defining objects. |
Objects inherit all the variables and methods from the class. | A class binds data(variables) as well as methods together as a single unit. |
with the objects being a "variable" of that type | A class can be considered as a type |
Method ( function ) use to state the behaviour of the class or object ( accelerate, brake, turn left ).
Attribute is used to describe the object/class state (colour, height, weight ).
Main Principles of OOP
Encapsulation - Wrapping of a class or hiding object attributes (state) with their methods (behaviour).
Abstraction - A process of hiding the implementation details and showing only the functionality to the user.
Inheritance - One object acquires all the properties and behaviour of the parent object
polymorphism - the ability to have one functionality in different ways.
Object-Oriented programming languages
Java
Python
C++
C#
Ruby
Advantages of OOP
Reusability- Code can be reused through inheritance, meaning a team does not have to write the same code multiple times.
Security - complex can be code is hidden, software maintenance is easier and internet protocols can be protected.
Easy maintenance - Programmers can implement system functionalities independently.
Disadvantages of OOP
OOP is more data centred, It doesn't focus enough on computation and algorithms.
memory consumption is higher than in other programming modules.