Object Oriented Programming with Python
Computer Science 2 Learning Outcome
Create and use Python classes that demonstrate inheritance and polymorphism.
Module Learning Outcomes
After completing this module, the student will be able to:
- Recognize definitions of key object-oriented concepts (object, property, method, class, constructor, instantiation, abstraction, encapsulation, inheritance, and polymorphism) and identify examples of them.
- Use Python syntax to create a new class and instantiate an object in that class.
- Demonstrate an understanding of inheritance by deriving a new child class from a parent class.
- Demonstrate an understanding of polymorphism by overriding one of the methods of a parent class in its child class.
Resources
Required
- Better Programming –
- W3Schools Tutorial –
- Traversy Media –
- Python Crash Course For Beginners [location 1:15:48-1:26:16]
Optional
- W3Schools Tutorial –
- Python 3.10.4 Documentation –
Agenda
Key Concepts [7 pts]
video [7:06] | transcript | code
- A class you already know and its methods
- Polymorphism in a method you’ve used
- How the Math Module provides additional Math methods and properties
- Knowledge Check #1: Identify examples of key concepts
Defining Classes [5 pts]
video [5:25] | transcript | code
- Create a new class
- Instantiate an object
- Knowledge Check #2: Instantiate another object
Inheritance [4 pts]
video [3:48] | transcript | code
- Create a child class from our parent class
- Call methods inherited from the parent class
- Add a method to the child class
- Knowledge Check #3: Create another child class
Polymorphism [4 pts]
video [1:58] | transcript | code
- Alter the behavior of a method in the child class
- Knowledge Check #4: Alter the behavior of a method in your child class
Assessments
There are two assessments for this content.
OOP Exam [20 pts]
Demonstrate Understanding of Key Concepts
Take the OOP Exam, which includes multiple choice and fill in the blank questions on key object-oriented concepts as implemented using Python. You have one attempt, unlimited time to complete the exam, and may use tutorials, videos, and tools for testing Python code during the exam. You may NOT consult with other humans about the exam while taking it.
OOP Code [60 pts]
Demonstrate Ability to Transfer Skills to New Tasks
Create and submit a file called name-oop.py, where name is your last name (e.g. Majchrzak-oop.py), which contains all of the following. You may consult tutorials, resources, members of your cohort, tutors, and your instructor for assistance in completing this task, but as always, create your own separate file. Do NOT share complete files with others! Duplicate file submissions will be flagged as plagiarism and earn a grade of zero. The goal is to understand and practice this material, so you can read, create, and use classes in your future programming endeavors.
- Class called Employee with the following
- A constructor that sets FName and LName properties to values sent when an object is instantiated
- Method called printLName which prints the value stored in LName
- Method called pay which prints the message “Pay depends on employee type.â€
- Class Hourly derived from class Employee with the pay method overridden, so it has two parameters (for hours worked & hourly wage) and returns the hours worked multiplied by the hourly wage
- Class Salary derived from class Employee with the pay method overridden, so it returns 700
- Class Commission derived from Employee with an additional property called comm that is set to 0.05. This is the commission rate. It should also override pay, so it has three parameters (hours worked, hourly wage, & total sales) and returns the sum of the hours multiplied by wage and the total sales multiplied by its comm property.
- An object variable called nCP instantiated by sending “Ada†and “LoveLace†to the commission class
- A statement setting a variable called payCheck to the value returned by nCP’s pay method when 20 hours are worked for a pay rate of $10 per hour and $3000 worth of sales were generated.
- A print statement printing the value stored in payCheck