Overview

Object-oriented programming (OOP) is the foundation of modern programming languages and it’s fundamental unit is the object. Around since the early 1960s, it did not start to gain momentum until the mid to late 1990s. Nonetheless, object-oriented languages are defined by the following three terms:

  • Encapsulation
  • Inheritance
  • Polymorphism

What is an object?

When you look at a person, you see the person as an object. And an object is defined by two components:

  • Attributes (Properties) - eye color, age, and height etc.
  • Behaviors (Methods) - walking, talking, and breathing etc.

In object-oriented design, both properties and methods are contained within a single object. Objects are the building blocks of an object-oriented program.

What is an object-oriented language?

Encapsulation

By combining the properties and methods in the same object, we have what is called encapsulation, which allows us to control access to the data in the object.

  • Property = data
  • Method = behavior
  • Object = entity
  • Class = template for an object (blueprint)

In general, two objects communicate with each other via their methods. Keep in mind that objects should not manipulate the internal data of other objects. It is best to build small objects with specific tasks versus building large objects that perform many.

  • The restriction of access to certain attributes and/or methods is called data hiding. For this to work, all attributes should be declared as private.
  • Objects are also known as instances. For example, a circle object is an instance of the circle class. In other words, a class is used to create an object. Thus, an object cannot be instantiated without a class!

Inheritance

Inheritance allows a class to inherit the properties and methods of another class. This allows the creation of brand-new classes by abstracting out common attributes and behaviors.

Polymorphism

Polymorphism is a Greek word that literally means many shapes. In object-oriented programming, we can use the same method, the same name, and same arguments to cause different things to happen according to the class in which we invoke a method. This feature is known as polymorphism.