0% found this document useful (0 votes)
8 views2 pages

Ai Lab

Class defines the attributes and behaviors that objects of that class will have. An object is an instance of a class with its own unique data. A constructor initializes an object's attributes when created. Encapsulation bundles data and methods into a class and restricts access to maintain integrity. Abstraction hides complex implementation details while exposing relevant features. Inheritance allows new classes to inherit attributes and methods from existing classes to promote code reuse.

Uploaded by

Muhmmad waqar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Ai Lab

Class defines the attributes and behaviors that objects of that class will have. An object is an instance of a class with its own unique data. A constructor initializes an object's attributes when created. Encapsulation bundles data and methods into a class and restricts access to maintain integrity. Abstraction hides complex implementation details while exposing relevant features. Inheritance allows new classes to inherit attributes and methods from existing classes to promote code reuse.

Uploaded by

Muhmmad waqar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Class:

Definition: A class is a blueprint or template for creating objects in object-oriented programming. It


defines the attributes (data members) and behaviors (methods) that the objects of the class will have.

Example:

In a "Car" class, you could define attributes like "color" and "model," and behaviors like
"start_engine" and "stop_engine."

Object:

Definition: An object is an instance of a class. It is a concrete realization of the class's blueprint,


with its own unique data and the ability to perform the methods defined in the class.

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:

Definition: A constructor is a special method in a class that is automatically called when an


object of the class is created. It is used to initialize the object's attributes or perform any setup
operations.

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:

Definition: Encapsulation is one of the fundamental principles of object-oriented


programming. It refers to the practice of bundling the data (attributes) and the methods (functions) that
operate on that data into a single unit called a class. This concept restricts access to certain parts of the
object, maintaining data integrity and preventing unauthorized external access.

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:

Definition: Abstraction is a fundamental concept in object-oriented programming that focuses on


hiding the complex implementation details of an object while exposing only the necessary and relevant
features to the outside world. It allows you to model real-world entities as abstract classes or interfaces,
defining a clear interface that can be used without knowing the internal workings.

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:

Definition: Inheritance is a core concept in object-oriented programming that allows you to


create new classes (derived or child classes) based on existing classes (base or parent classes). It enables
the child classes to inherit attributes and methods from the parent class, promoting code reuse and
hierarchy in class structures.

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.

You might also like