Object-Oriented Programming ( OOP )

Lakindu Banneheka
3 min readMar 4, 2023

--

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,

  1. classes
  2. 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.

// create a class
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.

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 | Class is declared once. |
| requirement. | |
|--------------------------------------------|----------------------------|
| Allocates memory when it is created. | Class doesn't allocate |
| | memory when it is created. |
|--------------------------------------------|----------------------------|
| An Object is one instance of a specific | A class is a template for |
| class. | defining objects. |
|--------------------------------------------|----------------------------|
| Objects inherit all the variables and | A class binds data as well |
| methods from the class. | as methods together as a |
| | single unit. |
|--------------------------------------------|----------------------------|
| With the objects being a "variable" of | A class can be considered |
| that type | as a type |
---------------------------------------------------------------------------

Method ( function ) use to state the behaviour of the class or object ( accelerate, brake, turn left ).

An attribute is used to describe the object/class state (colour, height, weight).

Main Principles of OOP

  1. Encapsulation - Wrapping of a class or hiding object attributes (state) with their methods (behaviour).
  2. Abstraction - A process of hiding the implementation details and showing only the functionality to the user.
  3. Inheritance - One object acquires all the properties and behaviour of the parent object
  4. 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.

--

--

Lakindu Banneheka

I'm a undergraduate in Electronic and computer science in university of Kelaniya, Sri Lanka.