CLICK
CLICK
3. Methods - are functions that are defined ➔ The __init__ special method, also
inside a class that describe the behaviors of known as Constructor, is used to
an object. Each method contained in class initialize
definitions starts with a reference to an
➔ the Book class with attributes such
as title, quantity, author, and price. ENCAPSULATION
➔ we can restrict access to methods
➔ The term self in the attributes refers and variables. This prevents data
to the corresponding instances from direct modification which is
(objects). called encapsulation. In Python, we
denote private attributes using
OBJECTS double underscores as the prefix.
➔ This class can be instantiated
(creating a copy of a class which ➔ If you want to access and change the
inherits all class variables and private variables, getter (getAge)
methods) to any number of objects. methods and setter methods
Three books are instantiated in the (setAge) should be used, as they are
following example code: part of Class.
INHERITANCE
METHODS
- a way of creating a new class for
➔ Methods are functions defined inside
using details of an existing class
the body of a class.
without modifying it. The newly
➔ They are used to define the behavior
formed class is a derived class (or
of an object.
child class). Similarly, the existing
class is a base class (or parent class).
CREATING METHODS IN PYTHON
➔ adds an instance method called
The derived or child class is the class that
display() to the Person class:
inherits. The base class or parent class is
➔ adds an instance method called
the class from which methods and/or
show() to the Student class:
attributes are inherited.
EXAMPLE
POLYMORPHISM
- the ability of an object to take many
forms. In simple words,
polymorphism allows us to perform
the same action in many different
ways.
- Polymorphism is taken from the
Greek words Poly (many) and
morphism (forms). Polymorphism
defines the ability to take different
forms.