Object Oriented Programming
Object Oriented Programming
CLASSES OBJECTS
- are user-defined data types that act as the - are instances of a class created with
blueprint for individual objects, attributes specifically defined data.
and methods. - Objects can correspond to real-world
- A class is a collection of objects. objects or an abstract entity.
- It is a logical entity that contains some - When class is defined initially, the
attributes and methods. description is the only object that is
- Classes define functions called methods, defined.
which identify the behaviors and actions - A class instance with a defined set of
that an object created from the class can properties
perform with its data. - the same class can be used to construct as
SOME POINT IN PYTHON CLASS many objects as needed.
Classes are created by keyword - The class and memory location of the
class. objects are printed when they are printed.
Attributes are the variables that We can't expect them to provide specific
belong to a class. information on the qualities but we can use
Attributes are always public and can a specific method called _ _repr_ _ (returns
be accessed using the dot (.) the object representation in string format) to
operator. do this.
DEFINING A CLASS
METHODS
- are functions that are defined inside a class
that describe the behaviors of an object.
- Each method contained in class definitions
starts with a reference to an instance
object.
- Subroutines contained in an object are
called instance methods. Programmers
use methods for reusability or keeping
functionality.
CREATING A METHOD
- The following adds an instance
method called display ()
- The following adds an instance
method called show ()
CLICK SHA1201-STRUCTURE
- To call an instance method, use the In the programming language, the + operator,
dot notation. acts as a concatenation and arithmetic addition.
NAME IN PYTHON
ATTRIBUTES
- are defined in the class template and Name also called an identifier is the name
represent the state of an object. given to the objects when we create objects.
- Objects will have data stored in the We can declare variable names and assign them
attributes field. to the objects as follows:
- Class attributes belong to the class itself. myInt = 11120
myString = “PythonProgram”
ENCAPSULATION
NAMESPACE
It is a collection of names and it is also a
It restricts access to methods and variables.
collection of defined symbolic names along with the
This prevents data from direct modification. We denote
information about the object that each name
private attributes using double underscores as the
references.
prefix.
It can be understood as a dictionary where a
If you want to access and change the private
name represents a key and objects are values.
variables, getter methods and setter methods should
Name (which means name, unique identifier) +
be used, as they are part of Class.
Space (related to scope)
GETTERS
- are the methods which help access the private
TYPES OF NAMESPACES
attributes or get the value of the private
attributes
SETTERS BUILT-IN NAMESPACE
- are the methods which help change or set the - contains the names of built-in functions and
value of private attributes. objects
- created while starting the python interpreter,
exists as long as the interpreter runs, and is
INHERITANCE
destroyed when we close the interpreter
- contains the names of built-in data types,
It is a way of creating a new class by using
exceptions, and functions like print() and
details of an existing class without modifying it
input().
DERIVED OR CHILD CLASS
GLOBAL NAMESPACE
- Newly formed class
- are defined at the program or module level
- is the class that inherits. - it contains the names of objects defined in a
BASE CLASS OR PAREN CLASS module or the main program
- Existing class - created when the program starts and exists
- is the class from which methods and/or until the program is terminated by the python
attributes are inherited. interpreter
The primary purpose of inheritance is the reusability of LOCAL NAMESPACE
code. Using inheritance, we can use the existing class - is defined for a class, function, loop, or any
to create a new class instead of recreating it from block of code
scratch. - the names defined in a block of code or a
function local to it
POLYMORPHISM - the variable names cannot be accessed outside
the block of code or the function in which they
Polymorphism in OOP is the ability of an object are defined.
to take many forms. In simple words, polymorphism - is created when the block of code or the
allows us to perform the same action in many different function starts executing and terminates when
ways. the function or the block of code terminates.
Polymorphism is taken from the Greek words ENCLOSING NAMESPACE
Poly (many) and morphism (forms).
CLICK SHA1201-STRUCTURE
- Function or a block of code defined inside any
function can access the namespace of the outer
function or block of code
- The outer namespace is termed as enclosing
namespace for the namespace of the inner
function or block of code