Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
Object-Oriented Programming (OOP) is a programming paradigm that uses objects and classes to organize code. The main ideas of OOP are based on the attempt to model the real world in programming code, using the concepts of objects that have properties (attributes) and behavior (methods).
Advantages of OOP
Modularity: Objects allow to encapsulate functionality and state, making the code more modular and easier to understand.
Inheritance: The ability to create a new class based on an existing one allows for code reuse and reduces duplication.
Polymorphism: The ability to use the same interface for different classes allows for extending functionality without modifying existing code.
Languages that Support OOP Paradigm
Java, C++, C#, Python, Ruby, Swift, Objective-C, Kotlin, PHP, JavaScript, Scala, Dart, Smalltalk, Eiffel, Simula, Go, Perl, Rust, Lua, Groovy, Haskell, Ada, Groovy, Delphi/Object Pascal, Fortran, R, ABAP, TypeScript, ActionScript, ColdFusion, PowerBuilder, Prolog, Visual Basic (VB.NET), F#, COBOL, PL/SQL, Io, Erlang, Lua, APL and many others.
Many languages can support multiple programming paradigms simultaneously. So this list is actually larger.
OOP Basics
Classes - are templates or blueprints for creating objects. A class defines the properties (attributes) and methods (functions) that an object will have.
An Object - is a specific instance of a class created using the class constructor. Each object has its own state (attribute values) and behavior (response to method calls).
Encapsulation in OOP means combining data and methods that operate on that data within a single object or class. Encapsulation allows for hiding the internal implementation and restricts access to certain components from the outside.
Inheritance allows for creating a new class based on an existing (parent) class, inheriting its properties and methods. This promotes the use of existing code and extends its functionality.
Polymorphism means the ability of objects of different classes to invoke the same method or function with the same name. This makes programs more flexible and generalized.
Example of Creating a Class and Object in Ruby
class Person
attr_accessor :name, :age
def initialize(name, age)
@name = name
@age = age
end
def introduce
puts "Hi, I'm #{@name} and I'm #{@age} years old."
end
end
# Creating an object of the Person class
person = Person.new("John", 25)
# Calling the object's method
person.introduce
This post doesn't have any additions from the author yet.