[go: up one dir, main page]

0% found this document useful (0 votes)
27 views3 pages

CLICK

Object oriented programming uses classes and objects to model real-world entities. Classes act as blueprints that define attributes and methods commonly shared by objects instantiated from the class. Key concepts include: - Classes define attributes like properties and behaviors like methods. Objects are instances of classes. - Inheritance allows a child class to inherit attributes and methods from a parent class while also adding its own. - Encapsulation restricts access to attributes and methods, improving modularity and security. - Polymorphism enables the same method to operate in different ways depending on the object type.

Uploaded by

Tisha Artagame
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)
27 views3 pages

CLICK

Object oriented programming uses classes and objects to model real-world entities. Classes act as blueprints that define attributes and methods commonly shared by objects instantiated from the class. Key concepts include: - Classes define attributes like properties and behaviors like methods. Objects are instances of classes. - Inheritance allows a child class to inherit attributes and methods from a parent class while also adding its own. - Encapsulation restricts access to attributes and methods, improving modularity and security. - Polymorphism enables the same method to operate in different ways depending on the object type.

Uploaded by

Tisha Artagame
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/ 3

OBJECT ORIENTED PROGRAMMING instance object.

Additionally, the subroutines


- is a programming paradigm that uses contained in an object are called instance
objects and classes in programming. methods. Programmers use methods for
It aims to implement real-world reusability or keeping functionality
entities like inheritance, encapsulated inside one object at a time.
polymorphisms, encapsulation, etc. in
the programming. 4. Attributes - are defined in the class
template and represent the state of an
- is to bind the data and the functions object. Objects will have data stored in the
that work on that together as a single attributes
unit so that no other part of the code field. Class attributes belong to the class
can access this data. itself.

An object has two characteristics: CLASS


1. attributes ● A class is a collection of objects.
2. behavior ● A class contains the blueprints or the
Let's take an example: prototype from which the objects are
- A parrot is an object, as it has the following being created. It is a logical entity
properties: that contains some attributes and
- name, age, color as attributes methods.
- singing, dancing as behavior ● Classes are used to create user-
defined data structures.
Main Concepts of OOP ● Classes define functions called
● Class methods, which identify the behaviors
● Objects and actions that an object created
● Polymorphism from the class can perform with its
● Encapsulation data.
● Inheritance
Some points in Python Class
Structure of OOP ● Classes are created by keyword class.
1. Classes – are user-defined data types that ● Attributes are the variables that
act as the blueprint for individual objects, belong to a class.
attributes and methods ● Attributes are always public and can
be accessed using the dot
2. Objects – are instances of a class created
with specifically defined data. Objects can
correspond to real-world objects or an
abstract entity. When class is defined
initially, the description is the only object
that is defined. we defined a class named Book.\

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.

➔ Getters are the methods which help


access the private attributes or get
the value of the private attributes and
book1, book2, and book3 are distinct objects
setters are the methods which help
of the class Book.
change or set the value of private
attributes.
➔ A class instance with a defined set of
properties is called an object. As a
➔ We will use a private attribute called
result, the same class can be used to
__discount in the Book class.
construct as many objects as needed.

➔ Employee we declare two variables


The class and memory location of the objects
name and __salary. We can observe
are printed when they are printed. We can't
that the name variable is accessible,
expect them to provide specific information
but __salary is the private variable.
on the qualities, such as the title, author
We cannot access it from outside of
name, and so on. But we can use a specific
class. If we try to access it, we will get
method called _ _repr_ _ (returns the object
an error.
representation in string format) to do this.

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

from a Vehicle (parent class), we created a


Car (child class). We don't need to define
common attributes and methods again in Car
class. We only need to add those attributes
and methods which are specific to the Car.

- In inheritance, the child class acquires


all the data members, properties, and
functions of the parent class.

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.

calculate_area() instance method created in


both Circle and Rectangle class. Thus, we
can create a method that takes any object
and calls the object's calculate_area()
method to implement polymorphism. Both
objects calculate the area (same action) but
in a different way (different formulas).

You might also like