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:

MyNumber=2
MyString=”abc”


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

1. Labels (“identifier”, “name”, “variable”)
—————> MyNumber and MyString
2. Objects – The objects are created when the program is executed.
—————> “2” and “abc” are values that belong to the objects.

Labels: MyNumber and My String
Objects: Have ID, Type, Value:

ID: 12345
type: Integer

value: 2

ID: 56789
type: String
value: abc

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

MyNumber—> 12345 | type Integer | value 2
MyString—> 56789 | type String | value “abc”


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

MyNumber—> 12345 | type Integer | value 2 <—-MyNewNumber


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
                  }