class Flashcards
is a programming Paradigm based on concept of “object”
object oriented programming
each object represents an entity with —– and ——-
attributes(data) and methods (behavior)
Key OOP principles:
- encapsulation
- Inheritance
- Polymorphism
- Abstraction
is a blueprint or template for creating objects in Python. It
encapsulates data (attributes) and functions (methods) that define
the properties and behavior of the objects.
- A class
does not occupy
memory until an object(instance) is created from it.
A class
Key concepts in a class using Python:
Attributes (Fields/Properties): These are variables that belong to the class.
They represent the data or state of an object. There are two types of attributes:
Instance attributes: Unique to each object.
v/ Class attributes: Shared across all instances of the class.
Methods: These are functions that belong to the class and define the
behaviors of the object. They typically operate on the object’s attributes.
Constructor (_init method): This is a special method in Python used to
initialize the attributes ofa class when an object is created.
Object (Instance): An object is an instance of a class. When a class is
defined, no memory is allocated until an object is created from it.
These are variables that belong to the class. They represent the data or state of an object.
Attributes (Fields/Properties):
There are two types of attributes:
Instance attributes: Unique to each object.
Class attributes: Shared across all instances of the class.
Unique to each object.
Instance attributes:
Shared across all instances of the class.
Class attributes:
These are functions that belong to the class and define the
behaviors of the object. They typically operate on the object’s attributes
Methods:
This is a special method in Python used to initialize the attributes of a class when an object is created.
Constructor (_init method):
When a class is
defined, no memory is allocated until an object is created from it.
Object (Instance):
why use classes in OOP:
Encapsulation: Classes allow you to bundle data (attributes)
and methods (functions) that operate on the data into one unit.
Reusability: You can create multiple objects (instances) from
a single class.
Inheritance: Classes can inherit attributes and methods from
other classes, promoting code reuse.
Polymorphism: Objects of different classes can be treated as
instances of the same superclass, even if they behave differently
Classes allow you to bundle data (attributes)
and methods (functions) that operate on the data into one unit.
Encapsulation:
You can create multiple objects (instances) from
a single class.
Reusability:
Classes can inherit attributes and methods from
other classes, promoting code reuse.
Inheritance:
Objects of different classes can be treated as
instances of the same superclass, even if they behave differently
Polymorphism:
is a special method called init ( ) in Python.
A constructor
It initializes an object’s attributes when the object is created.
A constructor
It is automatically invoked when an object is instantiated.
A constructor
is the concept of bundling data (attributes) and
methods that operate on the data within a class. It restricts direct
access to some attributes by using private (or protected)
members.Promotes data hiding and improves security by
preventing unauthorized access.
Encapsulation
allows one class (child class) to inherit attributes and
methods from another class (parent class).lt promotes code reuse
by letting new classes adopt behaviors of existing ones.Child
classes can also override parent class methods.
Inheritance
allows objects of different classes to be treated as
objects of a common superclass. It enables using a single
method across different object types.
Polymorphism
Types of polymorphism:
Method overriding: Subclasses can provide their own
implementation of methods inherited from the parent class.
Method overloading (not directly supported in Python but
can be simulated using default parameters).
Subclasses can provide their own
implementation of methods inherited from the parent class.
Method overriding:
(not directly supported in Python but
can be simulated using default parameters).
Method overloading