Intro to OOP

Object oriented programming

Object oriented programming (OOP) is a programming paradigm based on viewing problems as objects. Each object has attributes(characteristics) and methods(behaviours) that define what operations can be performed on these.



Classes

In OOP, a class is a definition or blueprint for creating objects of a certain type. A class defines all the attributes common to objects of that class. It also defines all the methods that can be used to access and modify those attributes.

The constructor

The constructor is the first method we define in a class. It is the method called when we create a new instance of an object. As this happens automatically when we make a call to the class it is known as a magic method. Like other magic methods it is preceded and followed by a double underscore. We pass it any values that are needed to populate the attributes of the new instance of the object. You will note we have also passed another value, self. This refers to this instance of an object.



Instantiation

The process of creating a new object of a class is to call the name of the class and pass any needed values. We don't pass anything for the self parameter. This is called creating a new instance of a class.

You can see in the example below that if I try to print the objects all I can see is the fact there is are two objects and some hexadecimal code. This shows a strong advantage of OOP which is information hiding. I can't directly see the vaues of an object's attributes. Below you can see that I can access the values for each object using dot notation, however this is not considered good practise in OOP.



Encapsulation

Accessing an objects attributes directly from outside the object itself violates the principle of encapsulation that is at the core of OOP. This says an object should encapsulate it's attributes and it's methods so it is fully self contained. This means you should only interact with an object from another object via its methods. This is the idea of information hiding in practice as one object cannot see the attributes of another other than via it's methods.

Getters and setters

Getters, sometimes called accessor methods, are used to return the value of an attribute for an external object. Setters, sometimes called mutator methods, are used to change the value of an attribute.



Understanding self

Self refers to this instance of the object. When I instantiate animal1 = animal("Fred","dog",7) animal1 is self. When I say animal2.getname() animal 2 is self. Self is the instance of the object we want the method applied to.



Universal modelling language

Universal modelling language(UML) is used to define object oriented programs. The UML diagram for a class is a box split into three sections with one for the class name, one for its attributes and their data types as well as a final box for its methods.



String methods

A string method is another magic method like the constructor method and shares the double underscore notation __str__. A string method can be given to an object to return its contents in an easy way for a user to view. If a an object has a string method, instead of the user seeing something like <__main__.animal object at 0x7fee97e37fd0> if it is printed they will be returned its string method.



Knowledge check


Questions:
Correct:

Question text


© All materials created by and copyright S.Goff