data-driven-docs

Selft training repo


Project maintained by ggranados Hosted on GitHub Pages — Theme by mattgraham

Object-Oriented Programming (OOP)


Table of Contents


What’s OOP

Object-Oriented Programming (OOP) is a programming paradigm in computer science that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages, including JavaScript, C++, Java, and Python.1

class-diagram.png

Back to top

OOP Concepts

The core concepts of object-oriented programming (OOP) include:

Encapsulation

Encapsulation is the principle of bundling data and the methods that operate on that data into a single unit called an object. It allows for data hiding and abstraction, ensuring that the internal representation and behavior of an object are hidden from the outside and can only be accessed through well-defined interfaces.

encapsulation.jpg

Back to top

Inheritance

Inheritance enables the creation of new classes (derived classes) based on existing classes (base or parent classes). Derived classes inherit properties and behaviors from their parent classes, allowing for code reuse and the creation of class hierarchies.

InheritanceinObjectOrientedProgramming.png

Back to top

Polymorphism

Polymorphism means the ability of objects to take on multiple forms or behave differently based on the context. It allows objects of different classes to be treated as instances of a common superclass, enabling flexibility and extensibility in code design.

There are two main types of polymorphism in object-oriented programming:

Static (Compile-time) Polymorphism:

Static polymorphism is achieved through method overloading and operator overloading.

Dynamic (Runtime) Polymorphism:

Dynamic polymorphism is achieved through method overriding and inheritance. Method overriding allows a subclass to provide a different implementation of a method that is already defined in its superclass. The decision on which implementation to execute is made at runtime based on the actual type of the object being referenced. This allows for the invocation of the appropriate method based on the specific object’s type during runtime. Dynamic polymorphism is commonly associated with the “IS-A” relationship, where a subclass is considered an instance of its superclass.

PolymorphisminObjectOrientedProgramming.png

Back to top

Abstraction

Abstraction involves focusing on essential characteristics and behavior while ignoring unnecessary details. It allows programmers to create abstract classes or interfaces that define a common set of properties and methods, without specifying their implementation. Abstraction helps in designing modular and maintainable code.

Back to top

Class

A class is a blueprint or template that defines the structure and behavior of objects. It encapsulates data (attributes) and methods (functions or operations) that manipulate that data. Objects are instances of classes.

Back to top

Object

An object is an instance of a class. It represents a particular entity with its own state (data) and behavior (methods). Objects interact with each other by invoking methods and exchanging messages.

Back to top

Message Passing

In OOP, communication between objects occurs through message passing. Objects send messages to other objects to request or provide information, invoking the appropriate methods to carry out the desired actions.

Back to top

OOP Languages

Back to top

Examples

The example defines a Rectangle class in Java, which has data (width and height) and behavior (calculateArea method) to calculate the area of a rectangle.

class Rectangle {
private int width;
private int height;

public Rectangle(int width, int height) {
        this.width = width;
        this.height = height;
    }
    
    public int calculateArea() {
        return width * height;
    }
}

Rectangle rectangle = new Rectangle(5, 7);
int area = rectangle.calculateArea();
System.out.println("Area: " + area);

Back to top


Ref.


Get Started | Paradigms | Java


  1. https://www.educative.io/blog/object-oriented-programming