Introduction

Object-Oriented programming is composed of four common practices, or pillars, of efficient programming skills and concepts. These include encapsulation, abstraction, inheritance, and polymorphism. When using a highly structured programming language, such as Java, learning these practices and implementing them into your code can make you a better, more effective, programmer.

If you’re brand new to programming, check out our beginner’s tutorial on Java programming first.

Encapsulation

Data encapsulation is the process of bundling data, functions, and properties using classes. A class is a blueprint for an object, or the collection of functions and variables. For example, let’s say you want to create a Dog object. You can create a class, or the blueprint to make a dog, by listing out the properties and functions of a Dog:

Properties:

  • Breed
  • Weight

Functions:

  • Walk
  • Bark
  • Sleep

By including all of these properties and functions into one class, you are practicing encapsulation. Check out this video on classes in Java for more information.

Encapsulation also includes using access modifiers to help simplify how code, and users, interact with the object. Access modifiers include making properties and functions private or public. For example, if you want a user to be able to make the dog object bark, you would use the public key word. If you did not want the user to make the dog sleep, you would use the private keyword. Learn more here about the different access modifiers and when to use them.

Let’s look at a coded example:

All of the properties and functions of a typical dog are contained in the Dog class. The functions, such as walk, bark, and sleep, are public, as well as the properties.

Abstraction

Similar to encapsulation, abstraction is also a process of hiding complex implementation details and showing only essential behavior of an object. Abstraction focuses on the WHAT of a function, not the HOW.

Let’s go back to the Dog object. To make the dog sleep, the dog has to find somewhere comfy, curl up, and close its eyes. But a user only cares to make the dog sleep, not knowing every step it takes to get to that state. By allowing a user to only interact with certain functions and properties, we simplify the user experience.

Let’s look at a coded example:

The details of the implementation, represented by pseudocode comments, are the details to be hidden from the user.

Inheritance

Inheritance promotes the practice of code reusability by implementing the details using an interface. An interface is simply a generic version of an object that contains one or several shared properties or methods that can be inherited by other classes.

For example, our Dog object has a sleep method. As discussed earlier, the dog finds somewhere comfy, curls up, and closes its eyes in order to sleep. A Cat object would do the same thing; find somewhere comfy, curl up, and close its eyes. Why write two pieces of identical code? Inheritance solves this with interfaces.

We can create an Animal interface that has the sleep function implementation. That way this function can be shared and implemented in the Dog, Cat, and possibly other Animal classes, eliminating the need to rewrite code. Check out this video to learn more about interfaces.

Let’s look at a coded example:

The Animal class contains a method for sleeping.

The Dog class extends the Animal class, inheriting the Sleep method without a need for implementation.

The Cat class also extends the Animal class, and doesn’t need implementation either.

Polymorphism

Polymorphism, meaning “many forms” , allows for a function or property to be inherited but implemented differently in various classes. Let’s look back to our example:

Both the Dog object and the Cat object make noises; a dog barks, and a cat meows. It’s the same function, but with different implementations, and different outcomes. We can create an interface that has flexible functions and properties, allowing them to be used differently between multiple classes.

Let’s look at a coded example:

The make noise function in the Animal Class returns nothing.

In the Dog class, the function returns “Bark!”.

In the Cat class, it is implemented differently, returning “Meow!”. When you call a dog to make noise, it will bark. When you call a cat to make noise, it will meow. But if you call any animal to make noise, it will do nothing.

For an advanced look at polymorphism, check out this article on method overloading and method overriding.

Summary & Further Resources

To review:

  1. Encapsulation: Compresses similar data and properties into one unit, also known as a class.
  2. Abstraction: Focusing on the WHAT and not the HOW by hiding implementation details from the user.
  3. Inheritance: Creates reusable code for similar classes using one general, inherited class, or an interface.
  4. Polymorphism: “Many forms”; or the implementation of one property or function differently between various classes.

All of these principles allow for more efficient programming and a better user experience. It’s important to learn these practices so that you can learn how to code effectively and productively. Check out some more resources on OOP and good programming practices:

More of our beginner-friendly articles: