Introduction to Object-Oriented Programming

Object-Oriented Programming (OOP)  is a type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure.

In this way, the data structure becomes an object that includes both data and functions. In addition, programmers can create relationships between one object and another. For example, objects can inherit characteristics from other objects.

The following topics will explain more details about the concepts used in OOP.

Classes and Objects

A class is simply a representation of a type of object, is the template from which individual objects are created. It is composed of three things: a name, attributes, and operations.

The abstract classes provide a common definition of a base class that can be shared by multiple derived classes. The sealed classes, on the other hand, provide complete functionality but cannot be used as base classes.

An object can be considered a "thing" that can perform a set of related activities. The set of activities that the object performs defines the object's behavior. Objects are self-contained data structures that consist of properties, methods, and events.

The following images explain the object-class relation. The Car class contains attributes and methods. You can create unlimited objects from the Car class.

Click to enlarge.

Encapsulation

The encapsulation is mainly achieved by creating classes, the classes expose public methods and properties. The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes. In that sense, encapsulation also allows a class to change its internal implementation without hurting the overall functioning of the system.

The idea of encapsulation is to hide how a class does it but to allow requesting what to do.

The following picture explain the concept of Encapsulation.



Inheritance

Inheritance is a feature of object-oriented programming that allows you to develop a class once, and then reuse that code over and over as the basis of new classes. Inheritance enables you to create new classes that reuse, extend, and modify the functionality defined in existing classes. 

The class that inherits the functionality is called a derived class, and the class whose functionality is inherited is called a base class. A derived class inherits all the functionality of the base class and can also define additional features that make it different from the base class.

Inheritance aids in the reuse of code. 

The following picture shows an example of inherance.

Click to enlarge.

Polymorphism

The word "polymorphism" means "many forms".

Polymorphism refers to a programming language's ability to process objects differently depending on their data type or class. More specifically, it is the ability to redefine methods for derived classes.

For example, given a base class shape, polymorphism enables the programmer to define different area methods for any number of derived classes, such as circles, rectangles and triangles. No matter what shape an object is, applying the area method to it will return the correct results.

The following picture shows an example of polymorphism.


Interfaces

An interface is a programming structure/syntax that allows the computer to enforce certain properties on an object (class).

For example, say we have a car class and a scooter class and a truck class. Each of these three classes should have a start_engine() action. How the "engine is started" for each vehicle is left to each particular class, but the fact that they must have a start_engine action is the domain of the interface.

An interface definition consists of a set of signatures for methods, properties, delegates, events, or indexers. An interface definition cannot consist of any data fields or any implementation details such as method bodies.

Conclusion 3

Object-oriented programming, or OOP, is a programming paradigm that represents concepts as "objects" that have data fields (the attributes that describe the object) and associated procedures known as methods.

Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs.

Some examples of object-oriented programming languages are C++ and Java.

The investigation of these concepts allow me to have a better understanding of the object-oriented programming, in order to prepare myself for the Software Development Fundamentals exam.