Python Class 25 - Python Object Oriented Programming Part-2

preview_player
Показать описание

Python Object Oriented Programming, Classes, Objects, Methods, Constructors, Inheritance, Polymorphism, Abstraction, and Encapsulation.

Object-oriented programming (OOP) refers to a type of computer programming in which programmers define the data type of a data structure, and also the types of operations (functions) that can be applied to the data structure.

Simula is considered the first object-oriented programming language, Smalltalk is considered the first truly object-oriented programming language.

The popular object-oriented languages are C++, Java, C#, PHP, Python, Ruby, Kotlin, etc.

Everything in Python is an object. An object has a state and behaviors. To create an object, you define a class first. And then, from the class, you can create one or more objects. The objects are instances of a class.

Python OOP Major Concepts and Principles:

Class
Object
Method
Inheritance
Polymorphism
Data Abstraction
Encapsulation

Python Complete Tutorial

Python Videos PlayList

Python Programming Syllabus

Python Programming Quiz

Python Interview Questions for Fresher
--------------------------------------------------
1. Introduction to Python Programming Language

2. Download and Install Python

Python Environment Setup (Using PyCharm IDE)

3. Python Language Syntax

4. Python Keywords and Identifiers

5. Comments in Python

6. Python Variables

7. Python Data Types

8. Python Operators

9. Python Conditional Statements

10. Python Loops

11. Python Branching Statements

12. Python Numbers

13. String Handling in Python

14. Python Data Structures - Lists

15. Python Data Structures - Sets

16. Python Data Structures - Tuples

17. Python Data Structures - Dictionaries

18. Python User Defined Functions

19. Python Built-in Functions

20. Python Modules

21. Python User Input

22. File Handling in Python

23. Python Date and Time

24. Python Exception Handling

25. Python Regular Expressions

26. Python Object Oriented Programming

27. Inheritance in Python

28. Polymorphism in Python

29. Abstraction in Python
--------------------------------------------------
Рекомендации по теме
Комментарии
Автор

Python Complete Tutorial

Python Videos PlayList

Python Programming Syllabus

Python Programming Quiz

Python Interview Questions for Fresher

1. Introduction to Python Programming Language

2. Download and Install Python

Python Environment Setup (Using PyCharm IDE)

3. Python Language Syntax

4. Python Keywords and Identifiers

5. Comments in Python

6. Python Variables

7. Python Data Types

8. Python Operators

9. Python Conditional Statements

10. Python Loops

11. Python Branching Statements

12. Python Numbers

13. String Handling in Python

14. Python Data Structures - Lists

15. Python Data Structures - Sets

16. Python Data Structures - Tuples

17. Python Data Structures - Dictionaries

18. Python User Defined Functions

19. Python Built-in Functions

20. Python Modules

21. Python User Input

22. File Handling in Python

23. Python Date and Time

24. Python Exception Handling

25. Python Regular Expressions

26. Python Object Oriented Programming

27. Inheritance in Python

28. Polymorphism in Python

29. Abstraction in Python

gcreddy
Автор

Class Notes:
Python Class 25: Python Object Oriented Programming Part-2

Object Oriented Programming Principles
1. Inheritance

2. Polymorphism in Python

Polymorphism – Many ways/forms

Polymorphism is an important concept in programming. It refers to the use of a single type entity (method, operator, or object) to represent different types in different scenarios.

Example 1: Polymorphism in addition operator:

The + operator is used extensively in Python programs. But, it does not have a single usage.

For number data types, + operator is used to perform arithmetic addition operation.

num1 = 10
num2 = 20
print(num1+num2)

num1 = 10.23
num2 = 20.45
print(num1+num2)

Similarly, for string data types, + operator is used to perform concatenation.

str1 = "Python "
str2 = "Programming"
print(str1+str2)

Example 2: Function Polymorphism in Python:

There are some functions in Python which are compatible to run with multiple data types.

One such function is the len() function. It can run with many data types in Python.

print (len("Python")) #6
print (len(["Python", "Java", "C", "C++", "Ruby", "Kotlin"])) #6
print (len({"Python", "Java", "C", 100, 10.23, True}))#6

Example 3: Polymorphism with Class Methods

class Country1():

def name(self):
print ("It is India")

def capital(self):
print ("Capital city is New Delhi")

class Country2():

def name(self):
print ("It is USA")

def capital(self):
print ("Capital city is Wasshigton, D.C")

object1 = Country1()
object2 = Country2()

object1.name()
object1.capital()
print("")
object2.name()
object2.capital()

class Class1():

def programmer(self):
print ("I am a Python Programmer")

class Class2(Class1):

def programmer(self):
print ("I am a Java Programmer")

obj = Class2()

obj.programmer()

3. Abstraction in Python

What is Abstraction?

Abstraction is one of the most important features of object-oriented programming. It is used to hide the implementation details.

In Python, we can achieve abstraction by incorporating abstract (incomplete) classes and methods.

Any class that contains an abstract (incomplete) method is called an abstract class.

Abstraction classes

In Python, abstraction can be achieved by using abstract classes and interfaces.

A class that consists of one or more abstract methods is called the abstract class. Abstract methods do not contain their implementation. An abstract class can be inherited by the subclass and the abstract method gets its definition in the subclass.

Example:

class Bikes:

def handle(self):
print ("Bikes have Handle")

def wheels(self):
pass

def engine(self):
pass

class HeroHonda(Bikes):

def wheels(self):
print ("Bikes have Spoke Wheels")

def engine(self):
print("Bikes have 4tX Engine")

obj1= HeroHonda()
obj1.handle()
obj1.engine()
obj1.wheels()

print("")

class Bajaj(Bikes):

def wheels(self):
print ("Bikes have Alloy Wheels")

def engine(self):
print("Bies have FT7 Engine")

obj2= Bajaj()
obj2.handle()
obj2.engine()
obj2.wheels()

Access Modifiers in Python

The access modifiers in Python are used to modify the default scope of variables. There are three types of access modifiers in Python: public, private, and protected.

Variables with the public access modifiers can be accessed anywhere inside or outside the class, the private variables can only be accessed inside the class, while protected variables can be accessed within the same package.

Access Modifiers
public var
private __var
protected _var

Constructor in Python

Constructors are generally used for instantiating an object. The task of constructors is to initialize(assign values) to the data members of the class when an object of the class is created.

In Python the __init__() method is called the constructor and is always called when an object is created.
Syntax of constructor declaration :

def __init__(self):
# body of the constructor

4. Encapsulation

Encapsulation in Python describes the concept of bundling data and methods within a single unit.

Accessing private fields via methods

class Car:

def __init__(self):
print ("Engine Started")
self.name ="Wegnar"
self.__make = "Maruthi"
self._model =2020
def myvar(self):
return self.__make

class myCar(Car):
pass

obj = myCar()
x = obj.name
print(x)

x=obj.myvar()
print(x)

gcreddy
join shbcf.ru