Python Class and Object

This article is summarizing the following video:


Assignment statement:
When an assignment is made an object is created.
Examples of assignment statements:

my_number = 1
my_string = "abc"


Parts of the assignment statement:
In the assignment statement there are labels and objects:

1. Labels – also called “identifier”, “name”, “variable”.

Labels:
 my_number, my_string 

2. Objects – The objects are created when the program is executed.

"2" and "abc" are NOT the objects.
They are the values of the objects.

Objects have ID, Type, Value:

                                                                      Objects

                                                                 ID:     12345
                                                                 type:   Integer
                                                                 value: 2

                                                                 ID:     56789
                                                                 type:  String
                                                                 value: ab


Binding of label to object:
During the assignment a label (“identifier”, “name”, “variable”) is BOUND to the object.

my_number ---> [  id: 12345  |  type: Integer  |  value: 2  ]
      my_string------> [  id: 56789  |  type: String   |   value: "abc"  ]


New variables:
When an object with a certain value already exists and a new label is assigned the same value a new object is NOT created.
The new label is bound to the existing object.

my_number---> [ id: 12345 | type: Integer | value: 2  ] <---- my_new_number

When a different value is assigned to one of the variables a new object is created.
It holds that new value and has a different ID:

my_number --------->  [ id: 12345   | type: Integer   |  value: 2  ]  <-- Object A
my_new_number ---> [  id: 17812  |  type: Integer  |  value: 3  ] <--- Object B

Code:

a = 1                      // An object with a value of 2 is created
b =  a                     // b is bound to the same object as a. 
                              // Because Python creates a new object only if the value is different.

id(a)                       // ---> 123, The IDs of both variables are the same.                              
id(b)                       // ---> 123. Both are bound to an object with the same value.
id(a) == id(b)        //  ---> TRUE. Compare by value.
b = 3                     // Assign new value to b.  Meaning a new object is created (new ID).
id(b)                      // --->789

id(a) == id(b)       // ---> FALSE. The variables are bound to two different objects

Abstract class vs. Interface

Abstract method: Only declared (not implemented) in the parent class. Descendants classes must implement.
Virtual method: Implemented in the class. Descendant classes may or may not override it.

Overriding a method: Descendant classes declare it with the same name + same arguments.
Overloading a method: Descendant classes declare and implement it with same name + different parameters.

Abstract class:
1. Must have at least 1 abstract method
2. Can have method declarations + implementations
3. Can not be used to create an object – only inherited (extended)
https://www.youtube.com/watch?v=O4irg48tGxA

Interface:
1. Has only abstract methods
2. Only method declarations (no implementation)
3. Can not be used to create an object – only inherited (implemented)
https://www.youtube.com/watch?v=ZkFj-39HUFA

Classes extending AC or implementing IF must:
A. Implement the abstract methods declared in AC
B. Implement all methods declared in the IF

Why the IF has only abstract methods (not implemented in the IF)?
Because the classes that implement the IF will be a great variety and not have a single common method.
But a constructor that accepts the IF can be passed any class that implements it.

Why AC is allowed to have method implementation?
Because the children classes can:
1. Inherit a common method 1 – have behavior common to all children.
2. Implement method 2 themselves – have behaviors specific for them.

Example:

abstract Class Animal

         method Move:
                   {
                       common for all animals 
                       => implemented in the abstract class Animal
                   }

        method MakeSound :
                  {
                     specific for every animal
                     => implemented in the classes extending Animal
                  }