Classes

Records contain a number of attributes.

An object contains attributes, and functions (called methods) to get or set these attributes.

UML Diagram for Example

Computer has a fixed ID and type. It has an operating system that can be updated.
Phone is a subclass type of computer, with an additional attribute and methods.

Computer        <-----   Phone
----------               ----------
- id : string;           - operator : string;
- type : string;         ----------
- os: string;            + getOperator()
----------               + changeOperator()
+ getID()
+ getType()
+ getOS()
+ updateOS()
Python Classes
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#define a new class
class Computer():
    # constructor method with parameters
    def __init__(self, id, type, os):
        #assign parameter values to object attributes
        self.id = id
        self.type = type
        self.os = os
         
        #initial attributes can be manipulated
        self.id = self.id.upper()
 
        # some attributes have default values
        self.previousOS = []
         
    #str method provides formatted output for print commands
    def __str__(self):
        str  = f"ID: {self.id:15} "
        str += f"Type: {self.type:10} "
        str += f"OS: {self.os}"
        str += f"\nPrevious OS: {self.previousOS}"
        return str
     
    # methods to get values of attributes (getters)
    def getID(self):
        return self.id
     
    def getType(self):
        return self.type
     
    def getOS(self):
        return self.os
     
    def getPreviousOS(self):
        return self.previousOS
     
    # methods to update attributes (setters)
    def updateOS(self, newOS):
        self.previousOS.append(self.os)
        self.os = newOS
         
# MAIN PROGRAM  
# instantiation of an object of the class
computer1 = Computer("AHS1913013","Desktop","Windows Vista")
print(computer1)
 
#use a setter method
computer1.updateOS("Windows 10 Professional")
print(computer1)
 
#use getter methods
id = computer1.getID()
type = computer1.getType()
print(f"Computer {id} is {type}")
ID: AHS1913013      Type: Desktop    OS: Windows Vista
Previous OS: []
 
ID: AHS1913013      Type: Desktop    OS: Windows 10 Professional
Previous OS: ['Windows Vista']
  
Computer AHS1913013 is Desktop
Python Subclasses
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# new class Phone is a subclass of Computer
class Phone(Computer):
    # polymorphism 1
    def __init__(self, id, os, operator):
        # call superclass contructor
        super().__init__(id, "Phone", os)
        # add subclass specific attributes
        self.operator = operator
     
    # polymorphism 2
    # used instead of superclass' method
    def __str__(self):
        str  = f"ID: {self.id:15} "
        str += f"Type: {self.type:10} "
        str += f"OS: {self.os:10} "
        str += f"Operator: {self.operator}"
        str += f"\nPrevious OS: {self.previousOS}"
        return str
     
    def getOperator(self):
        return self.operator
     
    def changeOperator(self, newOperator):
        self.operator = newOperator
 
# MAIN PROGRAM
computer1 = Computer("AHS1913013","Desktop","Windows Vista")
print(computer1)
  
phone1 = Phone("XYZ1234567","Android 8","EE")
print(phone1)
ID: AHS1913013      Type: Desktop    OS: Windows Vista
Previous OS: []
 
ID: XYZ1234567      Type: Phone      OS: Android 8  Operator: EE
Previous OS: []