Object-Oriented Programming Research Work
Brian Joseph Morales Obando
What is Object-Oriented Programming?
Object-Oriented Programming (OOP) is a programming paradigm that focuses on the use of objects to structure code. In OOP, objects represent real-world entities or concepts and contain both data and the functions or methods that operate on that data.
One quick example of OOP is a program that models a bank account. In this program, we might create an object called Account that represents a bank account. This Account object would contain data such as the account holder’s name, account number, and balance. It would also contain methods that allow us to deposit or withdraw money from the account, as well as methods to get information about the account.
Why is OOP important?
The Object-Oriented Programming (OOP) paradigm offers several benefits and advantages, making it an important programming approach in software development.
Modularity:
OOP allows for modular programming, which means that software can be broken down into smaller, more manageable modules. This makes it easier to develop, test, and maintain software, and enables developers to reuse code across different projects.
Reusability:
OOP encourages the reuse of code and reduces redundancy in software development. Developers can create classes and objects that can be reused across different projects, which saves time and reduces the risk of errors.
Encapsulation: OOP allows for encapsulation of data and code within objects, which helps to keep code organized and secure. It also helps to prevent unintended changes to data and code by restricting access to certain properties and methods.
Polymorphism:
OOP allows for polymorphism, which means that objects of different classes can be treated as if they were objects of the same class. This enables developers to write code that can work with different types of objects without having to know the specific type of object at compile time.
Flexibility:
OOP provides developers with the flexibility to change or update software without affecting other parts of the program. This is because each object is self-contained and can be modified independently of other objects.
Overall, OOP offers a powerful set of tools and techniques for software development that can help developers create high-quality, maintainable, and reusable software.
The four pillars of Object-Oriented Programming (OOP) are:
Encapsulation:
Encapsulation refers to the practice of bundling data and methods that operate on that data within a single unit, or object. Encapsulation helps to keep the data and the methods that operate on it together, which makes it easier to manage and modify.
Inheritance:
Inheritance allows one class to inherit properties and methods from another class. The class that inherits these properties and methods is called the subclass, while the class that provides them is called the superclass. Inheritance is a way to reuse code and reduce redundancy in a program.
Polymorphism:
Polymorphism allows objects of different classes to be treated as if they were objects of the same class. Polymorphism is useful for writing code that can work with different types of objects without having to know the specific type of object at compile time.
Abstraction:
Abstraction involves hiding the complexity of an object and showing only the essential features to the user. Abstraction helps to simplify code and make it easier to use.
Together, these pillars form the foundation of OOP and provide developers with a powerful set of tools for creating complex, reusable, and maintainable code.
Here are some basic concepts of Object-Oriented Programming (OOP):
Classes:
A class is a blueprint or template for creating objects that share common properties and behaviors. It defines the attributes and methods that an object will have but does not actually create any objects itself.
A class can be thought of as a user-defined data type that encapsulates related data and functions into a single unit. It represents a real-world entity or concept and can be used to create multiple instances of the same type of object.
In OOP, classes are determined by their properties and methods. Properties represent the data that an object contains, and methods represent the behaviors or actions that an object can perform. For example, a “Car” class might have properties like “make”, “model”, and “color”, and methods like “start_engine()” and “stop_engine()”.
The process of creating a class involves defining its properties and methods using a specific syntax that is dependent on the programming language being used. Once the class has been defined, it can be used to create objects or instances of that class by using the “new” keyword and passing any necessary parameters to the class constructor.
Objects: An object is an instance of a class. It is a tangible thing that has its own set of attributes and can perform its own set of behaviors. An example of an object might be an instance of the “Car” class called “myCar”, with properties like “make” = “Toyota”, “model” = “Corolla”, and “color” = “red”.
Methods and Encapsulation:
In Object-Oriented Programming (OOP), a method is a function or procedure defined in a class that represents a specific behavior or action that objects of that class can perform. Methods are used to define the behavior of objects and to implement the functionality of the program.
Methods are defined within a class and are associated with objects of that class. Each object of the class can call the methods defined in the class, and each object can have its own set of data that is used by the methods.
Encapsulation is a principle of OOP that refers to the practice of hiding the internal workings of a class from outside code. This is done to protect the integrity of the data and to ensure that the class can be used correctly.
Encapsulation is the practice of bundling data (attributes) and methods (behaviors) that operate on that data within a single unit, or object. Encapsulation helps to keep the data and methods that operate on it together, which makes it easier to manage and modify. An example of encapsulation might be the “myCar” object that has private attributes like “fuel_level” and “oil_level”, with public methods like “get_fuel_level()” and “change_oil()” that allow access to these attributes.
Inheritance:
Inheritance is a mechanism by which one class inherits properties and methods from another class. The class that inherits these properties and methods is called the subclass, while the class that provides them is called the superclass. Inheritance is a way to reuse code and reduce redundancy in a program. An example of inheritance might be a “SportsCar” class that inherits from the “Car” class, but has additional properties like “top_speed” and “acceleration”, and additional methods like “activate_nitro()”.
Polymorphism:
Polymorphism is the ability of objects of different classes to be treated as if they were objects of the same class. Polymorphism is useful for writing code that can work with different types of objects without having to know the specific type of object at compile time. An example of polymorphism might be a program that accepts different types of shapes (such as circles, squares, and triangles) as inputs and performs a calculation (such as calculating the area) that is specific to each shape.
Abstraction:
Abstraction involves hiding the complexity of an object and showing only the essential features to the user. Abstraction helps to simplify code and make it easier to use. An example of abstraction might be a “BankAccount” class that has methods like “deposit()” and “withdraw()”, but hides the details of how those operations are performed (such as which database or server is used to store the account information).
These concepts are fundamental to OOP and are used to create complex and reusable code. By understanding these concepts, programmers can create programs that are more efficient, easier to maintain, and easier to understand.
Object and Instance:
In Object-Oriented Programming (OOP), an object is a concrete instance of a class that contains data (attributes or properties) and behavior (methods or functions) defined by the class. Objects are created by instantiating a class, and they represent a specific instance or realization of that class.
Objects are formed by creating an instance of a class using the “new” keyword. For example, if we have a class called “Car”, we can create an object or instance of that class by instantiating it with the “new” keyword, like this:
Car myCar = new Car();
In this example, “myCar” is an object or instance of the “Car” class.
An instance is simply another term for an object. It refers to a specific occurrence or realization of a class. In other words, an instance is a concrete, unique object that is created from a class.
The terms “object” and “instance” are often used interchangeably in OOP, but it’s important to note that an object is a more general term that can refer to any entity in a program that has data and behavior, while an instance specifically refers to a concrete realization of a class.
Events:
An event is a signal that is generated by an object when a specific action occurs. Events are a way for objects to communicate with each other and for the program to respond to user actions or other external stimuli.
In many OOP languages, events are implemented using a system of event handlers or listeners. An event handler is a piece of code that is executed when an event occurs. When an event is generated, the program looks for the appropriate event handler to execute, and then runs the code associated with that handler.
Events are an important part of OOP because they allow objects to communicate with each other in a flexible and extensible way. By defining events and event handlers, we can create programs that respond dynamically to user actions and other external stimuli.
What is a Class Diagram (UML)?
Definition:
A Class Diagram is a type of Unified Modeling Language (UML) diagram used in Object-Oriented Programming (OOP) to represent the structure of a system by showing the classes of objects in the system, their attributes and operations, and the relationships between the classes.
In a Class Diagram, a class is represented as a rectangle that contains the class name, attributes, and operations. The attributes and operations of a class are represented as a list inside the rectangle, with the attributes listed first and the operations listed second.
Visibility:
Visibility is used to indicate the access level of a class member (i.e., an attribute or operation). There are three types of visibility in UML:
Public (+): A public member can be accessed from anywhere in the system.
Private (-): A private member can only be accessed from within the class that defines it.
Protected (#): A protected member can be accessed from within the class that defines it and its subclasses.
The visibility of a member is indicated by a symbol (+, -, or #) placed before the name of the member in the Class Diagram. For example, a public attribute called “name” would be represented as “+ name” in the Class Diagram, while a private operation called “doSomething” would be represented as “- doSomething”.
A protected member in UML is indicated by the symbol “#”. A protected member can be accessed from within the class that defines it and its subclasses. This means that subclasses can access protected members of their parent classes but other classes outside the hierarchy cannot.
For example, if a class called “Vehicle” has a protected attribute called “fuelLevel”, any subclasses of “Vehicle” (such as “Car” or “Motorcycle”) can access the “fuelLevel” attribute. However, a class outside of the “Vehicle” hierarchy would not be able to access this attribute.
Protected members are useful when you want to make certain attributes or methods available to subclasses but not to the rest of the system. They provide a way to restrict access to certain members while still allowing subclasses to inherit and use them.
Biography
“Object-Oriented Programming” on GeeksforGeeks: https://www.geeksforgeeks.org/object-oriented-programming-oops-concept-in-java/
“The Four Pillars of Object-Oriented Programming” on Medium: https://medium.com/@cancerian0684/the-four-pillars-of-object-oriented-programming-c4b1a16c5779
“Object-Oriented Programming Concepts” on TutorialsPoint: https://www.tutorialspoint.com/object_oriented_analysis_design/index.htm
“The Importance of Object-Oriented Programming” on CodeBurst: https://codeburst.io/the-importance-of-object-oriented-programming-c9f25d1eaaeb
“Classes and Objects” on Codecademy: https://www.codecademy.com/learn/learn-python-3/modules/learn-python3-classes/cheatsheet
“Object-Oriented Programming (OOP) Concepts in Python” on DataCamp: https://www.datacamp.com/community/tutorials/python-oop-tutorial
“Inheritance in Python” on Programiz: https://www.programiz.com/python-programming/inheritance
“Understanding Python Class Instantiation” on Real Python: https://realpython.com/python-class-instantiation/
“Polymorphism in Python” on Programiz: https://www.programiz.com/python-programming/polymorphism
“Methods in Python” on Programiz: https://www.programiz.com/python-programming/methods
“Encapsulation in Python: An Introduction” on Real Python: https://realpython.com/python-data-classes/#encapsulation-in-python
“Python Event-Driven Programming: Understanding and Implementing the Observer Pattern” on Medium: https://levelup.gitconnected.com/python-event-driven-programming-understanding-and-implementing-the-observer-pattern-8a31f60ec3e7
“Introduction to UML Class Diagrams” on Lucidchart: https://www.lucidchart.com/pages/uml-class-diagram
https://www.w3schools.com/ — for examples