Object Oriented Programming II

Object Oriented Programming (OOP) is a programming paradigm based on the concept of “objects.” For example, consider a remote-controlled toy car: the car itself is the object, and the remote control is the device that controls it. You can move the RC car forward, backward, left, and right using the remote, illustrating the basic concept of OOP. Python supports Object Oriented Programming through features like functions and classes.

Class

A class is a prototype, blueprint, or design that defines the variables and methods for specific objects. It serves to encapsulate the content of the program that will be executed, containing attributes/data types and methods to carry out a program. In Python, a class is defined using the keyword class, followed by the name of the class, like this: class class_name. Calling a class is similar to calling a function/method in a program by invoking the class name along with its parameters. Typically, a class contains many methods/functions that represent the properties of that class.

Classes can take various forms in different programming languages, such as abstract classes, data classes, and more. A class can also relate to other classes in a relationship known as inheritance, where a Parent Class (Base Class) has a Child Class (Derived Class) that inherits the same properties and variables.

For example, consider a car class: the car has attributes like engine, wheels, battery, etc. The car is a class, while the engine, wheels, battery, etc., are functionalities.

Important Aspects of Declaring a Class:

  • Initialization Function
  • Self Parameter

Initialization Function

This is a mandatory function that must be initialized when creating and declaring a class. It is used to add arguments and store parameters within the class.

Self Parameter

This is a variable that can only be used within the class declaration.

Simple Class Declaration Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Employee:
"Common base class for all employees"
empCount = 0

def __init__(self, name="Employee", salary=5000):
self.name = name
self.salary = salary
Employee.empCount += 1

def displayCount(self):
print("Total Employees: %d" % Employee.empCount)

def printEmployee(self):
print("Name:", self.name, "\nSalary:", self.salary)

employee1 = Employee("Azhar", 5000)

employee1.printEmployee()
employee1.displayCount()

employee2 = Employee("Gerald", 4000)

employee2.printEmployee()
employee2.displayCount()

Getter Method

This method retrieves data from the class. It is typically used when there is data that needs to be exported or accessed from the class.

You can use a getter method by creating a function that returns values in the class declaration.

Setter Method

This method changes data within the class. Sometimes setters are needed when dealing with immutable data or data that requires modification.

You can use the setter method similarly to declaring the init function in the class.

Example of Getter and Setter Methods:

1
2
3
4
5
6
7
8
9
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
class Employee:
"Common base class for all employees"
empCount = 0

def __init__(self, name="Employee", salary=5000):
self.name = name
self.salary = salary
Employee.empCount += 1

def displayCount(self):
print("Total Employees: %d" % Employee.empCount)

def printEmployee(self):
print("Name:", self.name, "\nSalary:", self.salary)

# Getter Method Declaration
def getEmployee(self, parameterType):
if parameterType == "Name":
return self.name
elif parameterType == "Salary":
return self.salary
else:
return "Data Not Found"

# Setter Method Declaration
def setEmployee(self, name, salary):
self.name = name
self.salary = salary

employee1 = Employee()

employee1.printEmployee()
employee1.displayCount()

# Example of Getter Implementation
employeeName = employee1.getEmployee("Name")
print("Employee's name is", employeeName)

# Example of Setter Implementation
employee1.setEmployee("Azhar", 50000)
employee1.printEmployee()

employee2 = Employee("Gerald", 4000)

employee2.printEmployee()
employee2.displayCount()

Exercise

Exercise 1

Create a program that implements a class named Student with methods to display the biodata of the student inputted by the user.

Exercise 2

Create a class that implements getter and setter methods, utilizing branching and looping as in the previous exercise. The program should accept user input for variable declarations and store it in a class, allowing manipulation and modification based on user input. The data can be displayed using the getter and setter methods.

Example Output:

1
2
3
4
5
6
7
8
9
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
===== OOP Program =====
1. Declare Object
2. Display Object
3. Change Object Value
4. Delete Object
5. Exit Program
Enter Your Choice (1/2/3/4/5): 2 (Input by User)

Name: None
Score: None

===== OOP Program =====
1. Declare Object
2. Display Object
3. Change Object Value
4. Delete Object
5. Exit Program
Enter Your Choice (1/2/3/4/5): 1 (Input by User)
Enter Your Name: ARZ (Input by User)
Enter Your Score: 95 (Input by User)
Data Successfully Added

===== OOP Program =====
1. Declare Object
2. Display Object
3. Change Object Value
4. Delete Object
5. Exit Program
Enter Your Choice (1/2/3/4/5): 2 (Input by User)

Name: ARZ
Score: 95

===== OOP Program =====
1. Declare Object
2. Display Object
3. Change Object Value
4. Delete Object
5. Exit Program

Enter Your Choice (1/2/3/4/5): 3 (Input by User)
What would you like to change (Name/Score): Score (Input by User)
Enter New Score: 100 (Input by User)
Score Data Successfully Changed

===== OOP Program =====
1. Declare Object
2. Display Object
3. Change Object Value
4. Delete Object
5. Exit Program

Enter Your Choice (1/2/3/4/5): 2 (Input by User)

Name: ARZ
Score: 100

===== OOP Program =====
1. Declare Object
2. Display Object
3. Change Object Value
4. Delete Object
5. Exit Program
Enter Your Choice (1/2/3/4/5): 4 (Input by User)
Data Successfully Deleted

===== OOP Program =====
1. Declare Object
2. Display Object
3. Change Object Value
4. Delete Object
5. Exit Program
Enter Your Choice (1/2/3/4/5): 2 (Input by User)

Name: None
Score: None

===== OOP Program =====
1. Declare Object
2. Display Object
3. Change Object Value
4. Delete Object
5. Exit Program
Enter Your Choice (1/2/3/4/5): 5 (Input by User)
Thank you for using my program.