Object-Oriented Programming (OOP) 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 the form of procedures (often known as methods). OOP aims to implement real-world entities like inheritance, polymorphism, encapsulation, and abstraction in programming.

Object-Oriented Programming gas three key aspects: i) it uses objects, not algorithms, as fundamental logical building blocks, ii each object is an instance of a class, and iii) classes can relate to each other via inheritance relationships. If any of these three elements is missing, the program is not truly object-oriented.

Key Concepts of OOP

Here’s a simple example to illustrate OOP concepts in Ruby:

# Define a class
class Animal
  attr_accessor :name

  def initialize(name)
    @name = name
  end

  def speak
    "Hello, I am an animal."
  end
end

# Define a subclass that inherits from Animal
class Dog < Animal
  def speak
    "Woof! My name is #{@name}."
  end
end

# Create an object of the Dog class
dog = Dog.new("Buddy")
puts dog.speak  # Output: Woof! My name is Buddy.

In this example:

Summary

Object-Oriented Programming (OOP) is a paradigm that uses “objects” to design applications and computer programs. It leverages key concepts like classes, objects, encapsulation, inheritance, polymorphism, and abstraction to create modular, reusable, and maintainable code.