Programming 2 OOP
Programming 2 OOP
Object-Oriented Programming
Object-Oriented Programming (OOP) is a paradigm for software development that organizes code into objects, which
are instances of classes, allowing for the creation of modular and reusable code. OOP fundamentally revolves
around the concept of objects, which represent real-world entities or concepts in a software system. These objects
encapsulate data and behavior, providing a way to model complex systems in a more manageable and intuitive
manner.
At the core of OOP are four key principles: encapsulation, inheritance, polymorphism, and abstraction. These
principles form the foundation upon which object-oriented systems are built, enabling developers to create robust,
flexible, and maintainable code.
Encapsulation is the practice of bundling data and methods that operate on that data within a single unit, called an
object. By encapsulating data, OOP hides the internal state of an object from external access, allowing for better
control over how data is manipulated and preventing unintended modifications. This enhances security and reduces
the risk of unintended side effects.
Inheritance is a mechanism that allows one class to inherit properties and behavior from another class. This
promotes code reuse and facilitates the creation of hierarchical relationships between classes. Through inheritance,
subclasses can extend and specialize the functionality of their parent classes, inheriting common attributes and
methods while adding new ones or overriding existing ones as needed.
Polymorphism enables objects of different classes to be treated as objects of a common superclass, allowing for
more flexible and modular code. Polymorphism allows methods to behave differently depending on the type of object
they are operating on, enabling dynamic dispatch and runtime flexibility. This enables developers to write code that is
more generic and adaptable to varying requirements.
Abstraction is the process of modeling complex systems by focusing on the essential aspects while hiding
unnecessary details. Abstraction allows developers to create simplified representations of real-world entities, making
it easier to understand and manage complex systems. By defining clear interfaces and hiding implementation details,
abstraction promotes modularity and code reuse while reducing complexity and coupling.
Together, these principles form the building blocks of object-oriented systems, enabling developers to create
software that is modular, extensible, and easy to maintain. OOP encourages a more intuitive and natural way of
thinking about software design by modeling systems in terms of objects and their interactions. This promotes better
organization, improves code readability, and facilitates collaboration among developers.
In addition to these principles, OOP also emphasizes concepts such as classes, objects, methods, properties, and
messages. Classes serve as blueprints for creating objects, defining their structure and behavior. Objects are
instances of classes, representing specific instances or occurrences of a concept. Methods are functions associated
with objects, defining their behavior and providing a way to interact with them. Properties are attributes of objects,
representing their state or characteristics. Messages are used to communicate between objects, enabling them to
interact and collaborate within a system.
Overall, Object-Oriented Programming provides a powerful and flexible approach to software development, enabling
developers to create systems that are modular, extensible, and easy to maintain. By organizing code into objects and
applying key principles such as encapsulation, inheritance, polymorphism, and abstraction, OOP promotes better
design, improved code quality, and increased productivity.
A Vague Overview of Different Programming Approaches
In conclusion, Object-Oriented Programming (OOP) in Python offers numerous advantages, including modularity,
reusability, extensibility, scalability, and maintainability. However, it also comes with certain disadvantages, such as
complexity, verbosity, performance overhead, limited control over memory management, and design complexity.
Understanding these trade-offs is essential for developers to make informed decisions about when and how to
leverage OOP principles in Python programming. Ultimately, the suitability of OOP depends on the specific
requirements and constraints of the project, as well as the preferences and expertise of the development team.
Classes in Python
In Python, classes serve as the blueprint for creating objects, allowing for the organization and structuring of code in
a modular and reusable manner. They facilitate the implementation of object-oriented programming (OOP) principles,
such as encapsulation, inheritance, and polymorphism. Understanding classes is fundamental to mastering Python's
OOP capabilities.
At its core, a class is a user-defined data type that bundles together data (attributes) and functions (methods) that
operate on that data. It provides a template for creating objects, which are instances of the class. Through classes,
you can model real-world entities or abstract concepts in your code, making it more intuitive and maintainable.
Line 1 is considered as class definition. In this line, the class Student is being defined.
Line 2 to Line 6 is called the constructor or the __init__ method. Note that functions inside a class are typically called
methods. The constructor has four parameters (name, age, course, year). The self.name, self.age, self.course, and
self.year are called instance variables for storing data that are being passed into the four parameters of the
constructor.
Line 8 to Line 9 is the instance for the display_info method. This method prints out the student’s information.
Line 11 to Line 16 is the instance for the promote method. This method promotes the student to the next academic
year. Unless the student passed the 12th grade, in that case the string “{name} has already graduated!” will be
printed.
Line 18 to Line 20 is the instane for the shift method. This method allows the student to shift to a new course which is
the string that is passed as a parameter.
Line 22 creates an instance of the Student class and making it into an object called student1.
Line 24 invokes the display_info method on the student1 object, printing the student’s information.
Activity #1
Promote the student by invoking the promote() method as shown in Line 26.
This means that for the student to graduate, you will have to invoke the promote() method 13 times. Write a Python
loop (while or for loop) and reduce the number of lines of code to just three. Paste your code in the Google
Classroom Activity.
Activity #2
Invoking the shift() method will change the student’s course to the string that was passed as a parameter. Create 9
more student objects and set their course to ‘BSIT’. Write a 3-line Python code that loop through all 10 students and
using the shift() method, change their course to “ComEng”. Paste your code in the Google Classroom Activity.