Ai Lab
Ai Lab
Example:
In a "Car" class, you could define attributes like "color" and "model," and behaviors like
"start_engine" and "stop_engine."
Object:
Example: If "Car" is a class, an object of this class could be a specific car, such as a "Toyota Camry," with
its unique color, model, and the ability to start and stop its engine.
Constructor:
Example: In Python, the __init__ method is a constructor. For the "Car" class, a constructor might set the
initial values of attributes like "color" and "model" when a new car object is created: def __init__(self,
color, model):.
Encapsulation in Python:
Example:
In Python, you can achieve encapsulation by defining class attributes as private, which are typically
prefixed with an underscore (e.g., _data). You can provide controlled access to these attributes by using
getter and setter methods within the class, allowing you to enforce constraints and validation. This
ensures that data is accessed and modified in a controlled manner, maintaining the object's internal
integrity.
Abstraction:
Example:
In Python, you can use abstraction by creating abstract classes using the abc module. For
instance, defining an abstract class "Shape" with a method "area" forces subclasses (e.g., "Circle" and
"Rectangle") to implement this method. Users of these classes can work with shapes without needing to
understand the specific details of each shape's area calculation.
Inheritance:
Example:
Consider a "Vehicle" class as the base class, and two child classes, "Car" and "Motorcycle," that inherit
from "Vehicle." The child classes inherit attributes like "color" and methods like "start" and "stop" from
the parent class while adding their own specific attributes and behaviors.