Object-oriented programming (OOPs)-python
OOPs Concepts:
An object-oriented programming is to design the program using classes and objects.
The object is related to real-word entities such as book, house, pencil, etc.
OOPs concept code writing is reusable code
The object is related to real-word entities such as book, house, pencil, etc.
OOPs concept code writing is reusable code
Principles of OOPs:
Class
Object
Method
Inheritance
Polymorphism
Data Abstraction
Encapsulation
Class:
Class can be define as collection of object.
The class have some specific attributes and methods.
For example: If you have school class, The it must contain an attribute and method
i.e an student name,roll num,email id,age, DOB
Example:
class className:
<statement -1>
. .
. .
<statement -N>
Object :
The object is an entity that has state and behaviour.
Everything in Python is an object,
Almost everything has attributes and methods.
All functions have a built-in attribute we define docstring it is source.
Example:
class school:
def __init__(self,name,age):
self.schname = name
self.studage = age
def display(self)
print(self.schname,studage)
cl = school("good",20)
cl.display()
Method :
Method is similar to function
The method is accessible to data that is contained within the class.
Example:
class className:
def method_name():
..........
def method_name():
...........
Method_body
...........
Inheritance :
Inheritance is the most important aspect of object-oriented programming
It provides the re-usability of the code.
Parent class :It is base class
child class :another class and also called child class
It provides the re-usability of the code.
Parent class :It is base class
child class :another class and also called child class
Example:
class India():
def capital(self):
print("New Delhi is the capital of India.")
def language(self):
print("Hindi is the most widely spoken language of India.")
def type(self):
print("India is a developing country.")
class USA():
def capital(self):
print("Washington, D.C. is the capital of USA.")
def language(self):
print("English is the primary language of USA.")
def type(self):
print("USA is a developed country.")
obj_ind = India()
obj_usa = USA()
for country in (obj_ind, obj_usa):
country.capital()
country.language()
country.type()
By this example we have two different class the we create for loop oof object the call the method.
Encapsulation:
Encapsulation is one of the fundamental concepts in object-oriented programming.
Encapsulation easy and also to secure the data.
Example:
class Person:
def __init__(self, name, age=0):
self.name = name
self.age = age
def display(self):
print(self.name)
print(self.age)
person = Person('Dev', 30)
person.display()
print(person.name)
print(person.age)
we will use different method to control of variable.
Data Abstraction:
Abstraction focuses on hiding the internal implementations of a process or method from the user.
An Object Oriented Programming, Inheritance, Polymorphism and Encapsulation go hand in hand. But Abstraction is also an essential element of OOP.
Example:
from abc import ABC, abstractmethod
class Animal(ABC):
def move(self):
pass
class Human(Animal):
def move(self):
print("I can walk and run")
class Snake(Animal):
def move(self):
print("I can crawl")
class Dog(Animal):
def move(self):
print("I can bark")
class Lion(Animal):
def move(self):
print("I can roar")
R = Human()
R.move()
K = Snake()
K.move()
R = Dog()
R.move()
K = Lion()
K.move()
Comments
Post a Comment