DEV Community

Cover image for Object Oriented Programming Concepts Explained
Travis Ramos
Travis Ramos

Posted on • Updated on • Originally published at travislramos.com

Object Oriented Programming Concepts Explained

What is Object Oriented Programming

Put simply, object oriented programming is the use of objects that contain both data and methods that interact with each other. For a language to be considered object oriented, it must have certain features like:

  • Encapsulation
  • Inheritance
  • Abstraction
  • Polymorphism

These features, along with a language having Objects and Classes, make up what we know as object oriented programming.

What is Encapsulation

Encapsulation is achieved when an object keeps its state private, so other objects don't have direct access to this state. Instead they must call a list of public methods to manipulate the state.

Take the photo below for example. The Cat has 3 different fields that are all private, plus a private meow() method. No other object that calls the Cat can change these values.

However, the Cat also has a list of public methods(Sleep, Play, and Feed). These public methods when called, will change the Cat's state as well as invoke the meow() method for Play and Feed.

Encapsulation Description

What is Inheritance

Inheritance is the creation of a hierarchy within your classes. You start off with a parent class, and then extend onto child classes which will reuse all of the fields and methods from the parent class plus any unique methods it implements.

Like the example below, the Teacher and the Student are both Person's, but one has a subject while the other has a school. Person is the parent class and Teacher & Student are the child classes.

Inheritance Description

What is Abstraction

Abstraction is the process in which an object only exposes itself at a high-level, and hides its internal implementation details. Other objects that are calling this don't care how things are being implemented, they just need the information to be processed.

Think of this as a small set of public methods that any other class can call wihout knowing how they are actually being implemented behind the scenes.

abstraction description image

Take the cell phone from the picture above for example. We don't know how the buttons on the phone are doing what they are doing, we just know what they do.

What is Polymorphism

Polymorphism means somthing occuring in multiple different forms. It allows objects of different types to be accessed through the same interface with each type implementing its own independent version of this interface.

Polymorphism Description

The star, circle, and rectangle are all Figure's, they are just different types of figures. They all have a calculateSurface() and calculatePerimeter() method. The difference is how they are actually doing this calculation.

Now Thats All Folks

Thanks for taking the time to read this post! If you found it useful, or think others would enjoy it, share it!

If you found this article helpful, subscribe to my newsletter where I'll be sending more content like this directly to your inbox on a weekly basis!

Top comments (0)