Object-Oriented Programming

My work through the module on Object-Oriented Programming.

Classes and Objects - Reflections

Learning outcomes:

1) A class has a collection of instructions on how to build its objects and how they behave. One of the most important part of every class is its constructor, which is a collection of object attributes. Creating an object we provide the values of these variables, unless they have a default value.

2) There is also possibility to implement a variable outside a constructor. Variables outside a constructor belong to the class. They are shared by all instances and any change applied to this variable causes the same change in all object of this class. Variables created inside a constructor belong to the object instance.

In Tutorial Labs about Classes and Objects I had to create a Person class with four variables. These variables are public (can be accessible form outside the class) and the example of that (iterating a list of objects and printing their names) was shown on Picture 1.

Class Functions and Methods - Reflections

Learning outcomes:
1. Functions are not associated with any objects and can be invoke by their names. They are independent elements of code. Methods, however, are associated with objects and they cannot be invoked only using their names. They are object dependent elements of code.

2. I have learnt that we can use a decorator (@classmethod) to make a class method, that changes the state of the whole class (not only its one object). If a method uses only class variables (outside a constructor) then it should have this decorator. This method has ‘cls’ as a parameter. It was used in challenge 1 about class functions and methods to print the actual value of count variable:

3. Static method (decorated with @staticmethod), however, is a general method that perform a task that is non-related to a class or an object. This method does not use ‘self’ or ‘cls’. I have used a static method in my System Implementation assignment. This method performs a task that is not related to its class and class’ attributes.

Inheritance - Reflections

Reflection:
Inheritance is one of the most important and commonly used relationship in object oriented programming. This concept is useful if we find similarity between classes (about data they carry, behaviour they have and especially if they have both of these). It helps to improve code elegance and better performance (code reuse). Based on information and knowledge I gained I could implement this approach in practice in Codio exercises. Considering one example: Person Class is a superclass for Adult Class. However, Adult Class is at the same time a superclass for Teacher. There is similarity between them but some changes and distinguishing elements were applied like adding one functionality in Adult Class (calc_bmi() method) and rewriting attributes in Teacher class.

Learning outcomes:
1. A subclass is a class that inheritance from a superclass, a construct with attributes as well as methods. Subclasses can be considered as a more specialize types of their Superclass.

2. Attributes and methods can be overwritten in subclasses to introduce some changes, but still keeping essential similarities to the superclass. I applied this functionalities in the exercise about Inheritance.

3. There is also a concept of multiple inheritance, in which a subclass inheritance form more than one super class. This approach however is not present in some programming languages (like Java, C# or Kotlin).

Encapsulation - Reflections

Encapsulation is a fundamental concept of object oriented programming. OOP gives us a chance to hide variables and some functionality (methods) in classes, in a way that they are not accessible form outside of the class. Some attributes or methods sometimes cannot be invoke form outside of classes, because they may carry important data that must not be overwritten, changed, manipulated (for example bank’s account balance). My own example of this approach is shown below (in Java):


Learning outcomes:

1. Encapsulation is used to hide data and functionality and prevent their changes or use outside the class.

2. In many programming languages it is possible to define attributes as private (like in Java), Python however is more flexible. There is a common practise of treating variables with one underscore as private (_var). But also it is possible to use double underscore convention (__var) for better protraction.

3. It is possible to get an access to private attributes by getters and change their values by setters. Base on Codio exercises I learnt two ways of doing it: property decorators or property function.

Polymorphism - Reflections

Polymorphism is a concept in which a function with its definition and logic may accept different types of variables and performing a specific job for a concrete type. The basic example describing this concept is provided below:

Function add_method can accept both integers as well as strings. In the first case there would be a mathematic operation (addition). In the second one concentrating a new, longer string. In object oriented programming polymorphism is similar, methods have different behaviour that depends on which subclass is used for the task. During my studies I familiarize myself with: method overriding, overloading, operation overloading, magic method and duck typing.

Learning outcomes:
1. Method overriding – a subclass overrides a method of superclass and acts differently form the original.
2. Method Overloading – if a class have methods with the same name but taking different paramiters and giving different outcomes.
3. Operator Overloading – in Python, the same operator may give different results based on what type of variables are passed.
4. Magic Methods are a set of special built-in functions in Python to manipulate objects. I have implemented this type of polymorphism in my System Implementation assignment, where I wanted to compare two objects and check if they have the same values of attributes:

5. Duck Typing – in this concept it does not matter what type of data iare passing to a method or function, as long as they can properly perform their job.

The exercises that have been done during the course: