Object Oriented programming explained

Let’s start off by defining Object Oriented Programming. According to Wikipedia, it’s is a programming paradigm based on the concept of “objects”, which can contain data, in the form of fields (often known as attributes or properties), and code, in and code, in the form of procedures (often known as methods).

There’s a lot that goes into object orientation, which we’ll get into later, but right now lets talk about what makes a language object oriented. Unlike other languages, OOP languages makes it possible to easily manipulate objects. This comes in handy for larger, complex and actively maintained programs. This is because OOP boosts speed and efficiency. Below’s the principles of OOP, feel free to practice these concepts in your favorite IDE.


Principles of Object Oriented Programming

Encapsulation

the process of binding the data by making it private and functions to process that data into a single unit. In other words, the concept of encapsulation is achieved when an object keeps its state private in a class. Other objects do not have access direct access to this state, they can only call a list of public functions or methods.

public class Vehicles {

//private data member
private String model;

//getter method for model
public String getModel() {
return model;
}

//setter method for model
public void setModel(String model) {
this.model = model;
} 
  }

Inheritance

When faced with similar objects (example: different types of cars that all have wheels), inheritance comes in handy. It allows developers to reuse a common logic while still maintaining a specific hierarchy. This means that you are able to create a class (child) by deriving from another class (parent) class. the child class can reuse all fields and methods of the parent class (similar parts) and can implement its own parts. Here’s an example.

//inheriting a class
class Child-class extends Parent-class {

//child class' own implements
}

Polymorphism

This is one of the most important features of OOP. It allows you to define one interface and have multiple implementations. In other words, It makes it possible to use a single action in different ways. A great way to remember this is using the IS-A-test. If an object passes the IS-A-test, then its considered to be polymorphic. Here’s an example.

//an interface
public interface Vehicle
{
  }
//class
public class wheels
{
  }
//polymorphic class
public class Carextends Vehicle implements wheels
{
  }

IS-A-test
Car IS-A Vehicle
Car IS-A 4 Wheels
Car IS-A car
Car IS-A Object

Abstraction

In OOP, the concept of abstraction is only revealing operations relevant for the other objects. It hides internal implementation details. There are two ways to achieve abstraction could be

  • Data Abstraction
    the way to create complex data types and exposing only meaningful operations to interact with data types, but hiding all the implementation details
  • Control Abstraction
    the process of identifying repeated statements and expose them as a unit of work.

These concepts are what make up a Object Oriented Program. Before wrapping this post up, I’d like to list some of the popular OOP languages below.

  • Java
  • C++
  • Python
  • JavaScript
  • PHP

That concludes this post, thanks for reading. Please feel free to share and comment below.

byaombe

One thought on “Object Oriented programming explained

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top