ECC Fall 2024 Lecture 2
ECC Fall 2024 Lecture 2
ECC Fall 2024 Lecture 2
Algorithms
● Objectives:
• Notebook available at
car_notebook
1) An internal representation:
● Achieved through data attributes that store the object's state and properties.
1) An internal representation
● If you were to manipulate the internal components of the car, such as directly
altering the engine parts or fuel lines, the car’s behavior might not work as expected.
● You’re in control of what data the class holds—whether it’s a car’s speed and
fuel level or a spaceship’s crew capacity and trajectory.
When you’re designing a class, you’re not just writing code—you’re establishing
the rules of the universe for that object type!
● Creating a Class:
● This step involves defining the structure and blueprint of the class itself.
● You determine the class name, its attributes (data it holds), and methods
(what it can do).
● For example, someone had to write the code to implement the list class in Python,
specifying how a list stores elements and the operations it supports, like sorting or
reversing.
● Creating a Class:
● This step involves defining the structure and blueprint of the class itself.
● You determine the class name, its attributes (data it holds), and methods
(what it can do).
● Using a Class:
● Once a class is defined, you can create instances (or objects) of that class.
● You can perform operations on these instances, using the methods defined
in the class.
● For example, creating a list instance with L = [1, 2] and calling len(L) to find its
length.
● Indentation is essential : all code within the class definition must be indented to
show that it belongs to the class.
● Indentation is essential : all code within the class definition must be indented to
show that it belongs to the class.
● The word object indicates that the class inherits from Python's base object class.
● This means that the class Car is a Python object and will automatically have access
to built-in attributes and methods.
● Data Attributes:
• Represent the internal state or properties of the class.
• These can be thought of as other variables or objects that define the characteristics
of each instance.
● When we define a class, we’re not creating an actual, tangible object yet.
● Instead, we’re building a blueprint or a template that outlines what the
object should look like and how it should behave.
● It’s a plan, a definition, but it doesn’t come to life until we use that
blueprint to create an instance.
● Conventional way
→ Equivalent to
Parameters including an
Name of the
object to call the method on
class Name of the method
● Getters and setters are special methods in OOP that provide controlled access to an
object's attributes.
● They encapsulate the internal state of an object, allowing safe interactions with its
data while maintaining the flexibility to make internal changes without affecting
external code.
● Consistent Interface
Getter method
allows external code
to read the value of
speed
Using a setter
++ allows to include validation logic
-- Slightly More Complex
● Vehicle class as the parent class and specific vehicle types like Car and Truck as
child classes.
● Vehicle class as the parent class and specific vehicle types like Car and Truck as
child classes.
Parent class
● Vehicle class as the parent class and specific vehicle types like Car and Truck as
child classes.
Define new
attributes
Define a new
method
● Vehicle class as the parent class and specific vehicle types like Car and Truck as
child classes.
Define a new
attribute
Define a new
method
• Creating Instances
• Using Methods:
• Multiple Inheritance allows a class to inherit attributes and methods from more than
one parent class.
• This is in contrast to single inheritance, where a class can inherit from only one
parent class.
• Multiple Inheritance allows a class to inherit attributes and methods from more than
one parent class.
• This is in contrast to single inheritance, where a class can inherit from only one
parent class.
HybridCar class inherits all the attributes and methods
from both Vehicle and Electric
defines a new
method
30/09/2024 ECC – Fall 2024 – Advanced Programming and Algorithms 39
k.z
Multiple inheritance and MRO
• Using Methods
• The Method Resolution Order (MRO) is a core concept in multiple inheritance that
defines the order in which classes are checked when a method is called on an object.
• Python first looks for the charge_battery() method in the HybridCar class.
• It is not found in HybridCar, so it moves to the next class in the MRO: Vehicle.
• The charge_battery() method is not found in Vehicle, so it moves to the next class
in the MRO: Electric.
• The charge_battery() method is found in Electric and executed, resulting in the
output: Battery is charging....
30/09/2024 ECC – Fall 2024 – Advanced Programming and Algorithms 44
k.z
Advanced Topics
● The term polymorphism comes from the Greek words "poly," meaning many, and
"morph," meaning form.
This means that you can call the same method on different types of objects, and each object
will respond in its own way, depending on its class definition.
● Method overriding occurs when a child class provides a specific implementation for a
method that is already defined in its parent class.
● Demonstrating Polymorphism
same method
Different outputs
● Demonstrating Polymorphism
● Demonstrating Polymorphism
same method
● Abstract Classes
● An abstract class is a class that cannot be instantiated directly.
● It serves as a blueprint for other classes.
● Abstract classes are designed to be inherited by subclasses that provide
concrete implementations of its abstract methods.
abstract method
decorated, every subclass
must implement this
method.
concrete method that is
shared by all subclasses.
30/09/2024 ECC – Fall 2024 – Advanced Programming and Algorithms 56
k.z
Abstract classes
● Inheritance:
● Both GasolineCar and ElectricCar inherit from Vehicle and provide specific implementations
of the abstract method start_engine().
● You cannot create an instance of the Vehicle class itself, as it is an abstract class !
● You can create instances of its subclasses (GasolineCar and ElectricCar) and call their
respective methods.
Abstract method
different behavior
● You can create instances of its subclasses (GasolineCar and ElectricCar) and call their
respective methods.
Concrete method