2022-05-15

Object Oriented Programming (OOP)

What is Object Oriented Programming (OOP)

Object-Oriented Programming (OOP) is a programming paradigm that provides a means of structuring programs so that properties and behaviors are bundled into individual objects. This is in contrast to procedural programming, where data and procedures are separate and the procedures operate directly on the data.

In OOP, each object is an instance of a class. The class defines what attributes (data) the object should have and what methods (functions) the object can perform. The object encapsulates both the data and the methods that operate on the data.

Fundamental Concepts in OOP

Class

A class is a blueprint or prototype from which objects are created. It represents a concept within the code, and includes properties (data) and methods (functions) common to all objects of this type.

Object

An object is an instance of a class. It is a self-contained entity that consists of both data and procedures to manipulate the data.

Inheritance

Inheritance is a fundamental principle that allows one class to inherit properties and methods from another class. The class that is inherited from is called the 'parent' or 'superclass', and the class that inherits is called the 'child' or 'subclass'. This relationship leads to a hierarchy and promotes reusability of code.

For instance, if we have a Vehicle class with properties like maker, model, color, and methods like start(), stop(), we can create a Car subclass which inherits these properties and methods and can also have additional properties (e.g., number_of_doors) and methods (e.g., openTrunk()).

Polymorphism

Polymorphism allows objects of different classes to be treated as objects of a common superclass. It enables one interface to represent a general class of actions. There are two types of polymorphism: compile-time (or static) and runtime (or dynamic).

Compile-time polymorphism is achieved through method overloading, where multiple methods have the same name but differ in the number or type of parameters. Runtime polymorphism is achieved through method overriding, where a subclass provides a specific implementation of a method that is already defined in its superclass.

Data Hiding and Encapsulation

Data hiding is a software development technique where only necessary fields or methods are exposed to the user. It's a way of reducing complexity and increasing efficiency. Encapsulation, closely related to data hiding, is the bundling of data and the methods that operate on that data.

These principles help to protect the data from accidental corruption and provide a way to control how data is accessed or modified. In other words, the user can manipulate the data only in the ways the designer intended.

Abstraction

Abstraction is the process of simplifying complex systems by modeling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem. This simplifies the problem domain, allows the code to be more maintainable and flexible, and reduces its complexity.

In OOP, abstraction is achieved through classes and interfaces. A class hides the complexity of tasks and provides a simple interface for the programming. For example, a Car class might have a start() method. While the method might involve many complex subtasks, these are abstracted away from the user, who only needs to call the start() method.

OOP in Various Languages

Java

Java is a statically-typed, object-oriented language that was designed with the principle of "Write Once, Run Anywhere" (WORA). In Java, everything is an object, with the exception of primitive types (int, char, float, etc.). Java supports all OOP principles including inheritance (through extends keyword), encapsulation (through public, private, and protected modifiers), polymorphism (through implements and override), and abstraction (through abstract classes and interfaces).

C++

C++ is a statically-typed, multiparadigm language that supports procedural, object-oriented, and generic programming. In C++, classes are used to create objects, and objects interact by sending messages to each other. Inheritance is implemented with : operator, and polymorphism is achieved through virtual functions. Encapsulation is implemented with public, private, and protected access specifiers. C++ does not support interfaces like Java, but similar functionality can be achieved through abstract classes.

Python

Python is a dynamic, high-level language that supports multiple programming paradigms, including procedural, object-oriented, and functional programming. In Python, everything is an object, including numbers, strings, and functions. Python supports all OOP principles. Classes are defined with the class keyword, and inheritance is implemented with parentheses in the class definition. Encapsulation is not enforced like in Java or C++, but a convention using underscore prefixes is used to indicate the privacy of attributes and methods.

JavaScript

JavaScript is a dynamic, high-level language primarily used for enhancing web interactivity. While it was not initially designed with classical OOP in mind, it can support OOP through its unique prototype-based model and, in more recent versions, through ES6 classes. JavaScript's approach to inheritance is based on prototypes rather than classes, and objects can inherit directly from other objects. ES6 introduced class syntax to JavaScript, bringing it syntactically closer to other OOP languages like Java and C++, but under the hood, it still operates on a prototype-based model.

Ryusei Kakujo

researchgatelinkedingithub

Focusing on data science for mobility

Bench Press 100kg!