Update classes.md

pull/442/head
Kaza.Sunitha 2024-05-19 11:58:39 +05:30 zatwierdzone przez GitHub
rodzic 33e3b84876
commit 11c37807e8
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 347 dodań i 294 usunięć

Wyświetl plik

@ -2,23 +2,36 @@
A class is a user-defined blueprint or prototype from which objects are created. Classes provides a means of bundling for data and functionality. Object is the instance of the class. A class can have any number of objects. A class is a user-defined blueprint or prototype from which objects are created. Classes provides a means of bundling for data and functionality. Object is the instance of the class. A class can have any number of objects.
**Syntax :** Class Definition **Syntax**
class ClassName:
#statements Class Definition:
class ClassName:
statements
**Syntax**
Object Definition:
**Syntax:** Object Definition
obj=ClassName() obj=ClassName()
The class creates a user-defined data structure, which holds its own data members and member functions which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object. The class creates a user-defined data structure, which holds its own data members and member functions which can be accessed and used by creating an instance of that class. A class is like a blueprint for an object.
**Some points on Python class:** **Some points on Python class:**
->Classes are created by keyword class.
->Attributes are the variables that belong to a class. * Classes are created by keyword class.
->Attributes are always public and can be accessed using the dot (.) operator.
* Attributes are the variables that belong to a class.
* Attributes are always public and can be accessed using the dot (.) operator.
Eg.: My class.attribute Eg.: My class.attribute
**Example of Python Class and object** **Example of Python Class and object**
Creating an object in Python involves instantiating a class to create a new instance of that class. This process is also referred to as object instantiation. Creating an object in Python involves instantiating a class to create a new instance of that class. This process is also referred to as object instantiation.
**Example:**
**Example**:
class Person: class Person:
def __init__(self, name, age): def __init__(self, name, age):
self.name = name self.name = name
@ -33,159 +46,146 @@ p1.myfunc()
Hello my name is John. Hello my name is John.
# The __init__() Function # The __init__() Function
All classes have a function called __init__(), which is always executed when the class is being initiated.We use the __init__() function to assign values to object properties. All classes have a function called __init__(), which is always executed when the class is being initiated.We use the __init__() function to assign values to object properties.
**Example:**
**Example**:
class Person: class Person:
def __init__(self, name, age): def __init__(self, name, age):
self.name = name self.name = name
self.age = age self.age = age
p1 = Person("John", 36) p1 = Person("John", 36)
Here when we created the p1 object with values "john" and 36 that are assigned to name ,age of __init__ function.
# The self Parameter # The self Parameter
The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class. The self parameter is a reference to the current instance of the class, and is used to access variables that belongs to the class.It does not have to be named self , you can call it whatever you like, but it has to be the first parameter of any function in the class.
**Example:**
**Example**:
class Person: class Person:
def __init__(self, name, age): def __init__(self, name, age):
mysillyobject.name = name self.name = name
mysillyobject.age = age self.age = age
def myfunc(abc):
print("Hello my name is " + abc.name)
p1 = Person("John", 36) p1 = Person("John", 36)
p1.myfunc() p1.myfunc()
When we call a method of this object as myobject.method(arg1, arg2), this is automatically When we call a method of this object as object.method(arg1, arg2), this is automatically converted by Python into Class.method(object, arg1, arg2).
converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.
# Concepts of Classes # Concepts of Classes
Main principles of OOPs in Python are abstraction, encapsulation, inheritance, and polymorphism. Main principles of OOPs in Python are abstraction, encapsulation, inheritance, and polymorphism.
->Abstraction - Abstraction
->Encapsulation - Encapsulation
->Inheritence - Inheritence
->Polymorphism - Polymorphism
# Abstraction # Abstraction
Data abstraction in Python is a programming concept that hides complex implementation details while Data abstraction in Python is a programming concept that hides complex implementation details while exposing only essential information and functionalities to users. In Python, we can achieve data abstraction by using abstract classes and abstract classes can be created using abc (abstract base class) module and abstractmethod of abc module.
exposing only essential information and functionalities to users. In Python, we can achieve data abstraction by using abstract classes and abstract classes can be created using abc (abstract base class) module and abstractmethod of abc module.
**Abstract Method:** **Abstract Method:**
In Python, abstract method feature is not a default feature. To create abstract method and abstract classes we have to import the “ABC” and “abstractmethod” classes from abc (Abstract Base Class) library. Abstract method of base class force its child class to write the implementation of the all abstract methods defined in base class. If we do not implement the abstract methods of base class in the child class then our code will give error. In the below code method_1 is a abstract method created using @abstractmethod decorator. In Python, abstract method feature is not a default feature. To create abstract method and abstract classes we have to import the “ABC” and “abstractmethod” classes from abc (Abstract Base Class) library. Abstract method of base class force its child class to write the implementation of the all abstract methods defined in base class. If we do not implement the abstract methods of base class in the child class then our code will give error. In the below code method_1 is a abstract method created using @abstractmethod decorator.
**Syntax:**
**Syntax**:
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
class BaseClass(ABC): class BaseClass(ABC):
@abstractmethod @abstractmethod
def method_1(self): def method_1(self):
#empty body #statements
**Implementation of Data Abstraction in Python**
In the below code, we have implemented data abstraction using abstract class and method. Firstly, we import the required modules or classes from abc library then we create a base class Car that inherited from ABC class that we have imported. Inside base class we create init function, abstract function and non-abstract functions. To declare abstract function printDetails we use “@abstractmethod” decorator. After that we create child class hatchback and suv. Since, these child classes inherited from abstract class so, we need to write the implementation of all abstract function declared in the base class. We write the implementation of abstract method in both child class. We create an instance of a child class and call the printDetails method. In this way we can achieve the data abstraction.
**Example:**
**Import required modules**
from abc import ABC, abstractmethod
**Create Abstract base class** **Implementation of Data Abstraction in Python**
In the below code, we have implemented data abstraction using abstract class and method. Firstly, we import the required modules or classes from abc library then we create a base class Car that inherited from ABC class that we have imported. Inside base class we create init function, abstract function and non-abstract functions. To declare abstract function printDetails we use “@abstractmethod” decorator. After that we create child class hatchback and suv. Since, these child classes inherited from abstract class so, we need to write the implementation of all abstract function declared in the base class. We write the implementation of abstract method in both child class. We create an instance of a child class and call the printDetails method. In this way we can achieve the data abstraction.
**Example**:
from abc import ABC, abstractmethod
class Car(ABC): class Car(ABC):
def __init__(self, brand, model, year): def __init__(self, brand, model, year):
self.brand = brand self.brand = brand
self.model = model self.model = model
self.year = year self.year = year
**Create abstract method**
@abstractmethod @abstractmethod
def printDetails(self): def printDetails(self):
pass pass
**Create concrete method**
def accelerate(self): def accelerate(self):
print("speed up ...") print("speed up ...")
def break_applied(self): def break_applied(self):
print("Car stop") print("Car stop")
**Create a child class**
class Hatchback(Car): class Hatchback(Car):
def printDetails(self): def printDetails(self):
print("Brand:", self.brand); print("Brand:", self.brand);
print("Model:", self.model); print("Model:", self.model);
print("Year:", self.year); print("Year:", self.year);
def Sunroof(self): def Sunroof(self):
print("Not having this feature") print("Not having this feature")
**Create a child class**
class Suv(Car): class Suv(Car):
def printDetails(self): def printDetails(self):
print("Brand:", self.brand); print("Brand:", self.brand);
print("Model:", self.model); print("Model:", self.model);
print("Year:", self.year); print("Year:", self.year);
def Sunroof(self): def Sunroof(self):
print("Available") print("Available")
car1 = Hatchback("Maruti", "Alto", "2022"); car1 = Hatchback("Maruti", "Alto", "2022");
car1.printDetails() car1.printDetails()
car1.accelerate() car1.accelerate()
**OUtput** **OUtput:**
Brand: Maruti Brand: Maruti
Model: Alto Model: Alto
Year: 2022 Year: 2022
speed up ... speed up ...
# Encapsulation # Encapsulation
Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an objects variable can only be changed by an objects method. Those types of variables are known as private variables. Encapsulation is one of the fundamental concepts in object-oriented programming (OOP). It describes the idea of wrapping data and the methods that work on data within one unit. This puts restrictions on accessing variables and methods directly and can prevent the accidental modification of data. To prevent accidental change, an objects variable can only be changed by an objects method. Those types of variables are known as private variables.
A class is an example of encapsulation as it encapsulates all the data that is memberfunctions, variables, etc. The goal of information hiding is to ensure that an objects state is always valid by controlling access to attributes that are hidden from the outside world. A class is an example of encapsulation as it encapsulates all the data that is memberfunctions, variables, etc. The goal of information hiding is to ensure that an objects state is always valid by controlling access to attributes that are hidden from the outside world.
**Encapsulation in Python** **Encapsulation in Python**
class=Methods + Variables class=Methods + Variables
Encapsulating the methods and variables in class i.e that methods and variables are related and they are written in a class.Encapsulation reduces the code and one can create any number of objects and use them. Encapsulating the methods and variables in class i.e that methods and variables are related and they are written in a class.Encapsulation reduces the code and one can create any number of objects and use them.
# Protected Members # Protected Members
Protected members (in C++ and JAVA) are those members of the class that cannot be accessed outside the class but can be accessed from within the class and its subclasses. To accomplish this in Python, just follow the convention by prefixing the name of the member by a single underscore “_”.
**Example:**
**Creating a base class** Protected members (in C++ and JAVA) are those members of the class that cannot be accessed outside the class but can be accessed from within the class and its subclasses. To accomplish this in Python, just follow the convention by prefixing the name of the member by a single underscore “_”.
**Example**:
class Base: class Base:
def __init__(self): def __init__(self):
# Protected member # Protected member
self._a = 2 self._a = 2
**Creating a derived class**
class Derived(Base): class Derived(Base):
def __init__(self): def __init__(self):
# Calling constructor of
# Base class
Base.__init__(self) Base.__init__(self)
print("Calling protected member of base class: ", print("Calling protected member of base class: ", self._a)
self._a)
# Modify the protected variable:
self._a = 3 self._a = 3
print("Calling modified protected member outside class: ", print("Calling modified protected member outside class: ",
self._a) self._a)
obj1 = Derived() obj1 = Derived()
obj2 = Base() obj2 = Base()
print("Accessing protected member of obj1: ", obj1._a) print("Accessing protected member of obj1: ", obj1._a)
print("Accessing protected member of obj2: ", obj2._a) print("Accessing protected member of obj2: ", obj2._a)
**output** **output**:
Calling protected member of base class: 2 Calling protected member of base class: 2
Calling modified protected member outside class: 3 Calling modified protected member outside class: 3
Accessing protected member of obj1: 3 Accessing protected member of obj1: 3
Accessing protected member of obj2: 2 Accessing protected member of obj2: 2
# Private Member # Private Member
Private members are similar to protected members, the difference is that the class members declared private should neither be accessed outside the class nor by any base class. In Python, there is no existence of Private instance variables that cannot be accessed except inside a class.
Private members are similar to protected members, the difference is that the class members declared private should neither be accessed outside the class nor by any base class. In Python, there is no existence of Private instance variables that cannot be accessed except inside a class.
However, to define a private member prefix the member name with double underscore “__”. However, to define a private member prefix the member name with double underscore “__”.
**Example:**
**Example**:
class Base: class Base:
def __init__(self): def __init__(self):
self.a = "GeeksforGeeks" self.a = "GeeksforGeeks"
self.__c = "GeeksforGeeks" self.__c = "GeeksforGeeks"
**Creating a derived class**
class Derived(Base): class Derived(Base):
def __init__(self): def __init__(self):
Base.__init__(self) Base.__init__(self)
@ -193,19 +193,23 @@ class Derived(Base):
print(self.__c) print(self.__c)
obj1 = Base() obj1 = Base()
print(obj1.a) print(obj1.a)
# output **output**:
It will raises error because private members cannot be used outside the class even not for derived classes It will raises error because private members cannot be used outside the class even not for derived classes
# Inheritence # Inheritence
->Inheritance allows us to define a class that inherits all the methods and properties from another class.
->Parent class is the class being inherited from, also called base class. - Inheritance allows us to define a class that inherits all the methods and properties from another class.
->Child class is the class that inherits from another class, also called derived class. - Parent class is the class being inherited from, also called base class.
- Child class is the class that inherits from another class, also called derived class.
**Creating of Parent Class** **Creating of Parent Class**
Any class can be a parent class, so the syntax is the same as creating any other class:
**Example** Any class can be a parent class, so the syntax is the same as creating any other class.
**Example**:
class Person: class Person:
def __init__(self, fname, lname): def __init__(self, fname, lname):
self.firstname = fname self.firstname = fname
@ -213,22 +217,24 @@ class Person:
def printname(self): def printname(self):
print(self.firstname, self.lastname) print(self.firstname, self.lastname)
#Use the Person class to create an object, and then execute the printname method:
x = Person("John", "Doe") x = Person("John", "Doe")
x.printname() x.printname()
**output** **output**:
John Doe John Doe
**Creating of Child Class** **Creating of Child Class**
To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class: To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:
**Example** **Example**:
class Student(Person): class Student(Person):
pass pass
# Super Function # Super Function
Python also has a super() function that will make the child class inherit all the methods and properties from its parent: Python also has a super() function that will make the child class inherit all the methods and properties from its parent:
**Example:** **Example**:
class Person: class Person:
def __init__(self, fname, lname): def __init__(self, fname, lname):
self.firstname = fname self.firstname = fname
@ -243,24 +249,29 @@ class Student(Person):
x = Student("Mike", "Olsen") x = Student("Mike", "Olsen")
x.printname() x.printname()
**output:** **output**:
Mike Olsen Mike Olsen
**Types of Inheritances** **Types of Inheritances**
Types of Inheritance depend upon the number of child and parent classes involved. There are four types of inheritance in Python. Types of Inheritance depend upon the number of child and parent classes involved. There are four types of inheritance in Python.
They are: They are:
->Single Inheritance - Single Inheritance
->Multiple Inheritance - Multiple Inheritance
->Multilevel Inheritance - Multilevel Inheritance
->Hierarchical Inheritance - Hierarchical Inheritance
->Hybrid Inheritance - Hybrid Inheritance
**Single Inheritance** **Single Inheritance**
Single inheritance enables a derived class to inherit properties from a single parent class, thus enabling code reusability and the addition of new features to existing code. Single inheritance enables a derived class to inherit properties from a single parent class, thus enabling code reusability and the addition of new features to existing code.
**Single Inheritance** **Single Inheritance**
class A class A
| |
V V
class B class B
**Example** **Example**:
class Parent: class Parent:
def func1(self): def func1(self):
@ -273,17 +284,23 @@ class Child(Parent):
object = Child() object = Child()
object.func1() object.func1()
object.func2() object.func2()
**output** **output**:
This function is in parent class. This function is in parent class.
This function is in child class. This function is in child class.
**Multiple Inheritance** **Multiple Inheritance**
When a class can be derived from more than one base class this type of inheritance is called multiple inheritances. In multiple inheritances, all the features of the base classes are inherited into the derived class. When a class can be derived from more than one base class this type of inheritance is called multiple inheritances. In multiple inheritances, all the features of the base classes are inherited into the derived class.
**Multiple Inheritance** **Multiple Inheritance**
class A Class B class A Class B
\ / \ /
\ / \ /
Class C Class C
**Example** **Example** :
class Mother: class Mother:
mothername = "" mothername = ""
def mother(self): def mother(self):
@ -300,11 +317,16 @@ s1 = Son()
s1.fathername = "RAM" s1.fathername = "RAM"
s1.mothername = "SITA" s1.mothername = "SITA"
s1.parents() s1.parents()
**output:** **output**:
Father : RAM Father : RAM
Mother : SITA Mother : SITA
**Multilevel Inheritance** **Multilevel Inheritance**
In multilevel inheritance, features of the base class and the derived class are further inherited into the new derived class. This is similar to a relationship representing a child and a grandfather. In multilevel inheritance, features of the base class and the derived class are further inherited into the new derived class. This is similar to a relationship representing a child and a grandfather.
**Multilevel Inheritance** **Multilevel Inheritance**
class A class A
| |
@ -313,7 +335,8 @@ In multilevel inheritance, features of the base class and the derived class are
| |
V V
class C class C
**Example:** **Example**:
class Grandfather: class Grandfather:
def __init__(self, grandfathername): def __init__(self, grandfathername):
@ -338,26 +361,39 @@ class Son(Father):
s1 = Son('Prince', 'Rampal', 'Lal mani') s1 = Son('Prince', 'Rampal', 'Lal mani')
print(s1.grandfathername) print(s1.grandfathername)
s1.print_name() s1.print_name()
# output **output**:
Lal mani Lal mani
Grandfather name : Lal mani Grandfather name : Lal mani
Father name : Rampal Father name : Rampal
Son name : Prince Son name : Prince
# Polymorphism # Polymorphism
The word polymorphism means having many forms. In programming, polymorphism means the same function name (but different signatures) being used for different types. The key difference is the data types and number of arguments used in function. The word polymorphism means having many forms. In programming, polymorphism means the same function name (but different signatures) being used for different types. The key difference is the data types and number of arguments used in function.
**Example of python built in functions**
**Example of python built in functions**:
#len() being used for a string #len() being used for a string
print(len("geeks")) print(len("geeks"))
#len() being used for a list #len() being used for a list
print(len([10, 20, 30])) print(len([10, 20, 30]))
**output** **output**:
5 5
3 3
**Polymorphism with class methods:**
**Polymorphism with class methods**:
Polymorphism with class methods means different classes may have the methods with same method names.So here the polymorphism applies when the object is created for a particular class,the method related to that is invoked. Polymorphism with class methods means different classes may have the methods with same method names.So here the polymorphism applies when the object is created for a particular class,the method related to that is invoked.
**Example**
**Example**:
class India(): class India():
def capital(self): def capital(self):
print("New Delhi is the capital of India.") print("New Delhi is the capital of India.")
@ -384,16 +420,27 @@ for country in (obj_ind, obj_usa):
country.capital() country.capital()
country.language() country.language()
country.type() country.type()
#Output:
**Output**:
New Delhi is the capital of India. New Delhi is the capital of India.
Hindi is the most widely spoken language of India. Hindi is the most widely spoken language of India.
India is a developing country. India is a developing country.
Washington, D.C. is the capital of USA. Washington, D.C. is the capital of USA.
English is the primary language of USA. English is the primary language of USA.
USA is a developed country. USA is a developed country.
**Polymorphism with Inheritance** **Polymorphism with Inheritance**
In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. However, it is possible to modify a method in a child class that it has inherited from the parent class. This is particularly useful in cases where the method inherited from the parent class doesnt quite fit the child class. In such cases, we re-implement the method in the child class. This process of re-implementing a method in the child class is known as Method Overriding. In Python, Polymorphism lets us define methods in the child class that have the same name as the methods in the parent class. In inheritance, the child class inherits the methods from the parent class. However, it is possible to modify a method in a child class that it has inherited from the parent class. This is particularly useful in cases where the method inherited from the parent class doesnt quite fit the child class. In such cases, we re-implement the method in the child class. This process of re-implementing a method in the child class is known as Method Overriding.
**Example:**
**Example**:
class Bird: class Bird:
def intro(self): def intro(self):
print("There are many types of birds.") print("There are many types of birds.")
@ -421,12 +468,18 @@ obj_spr.flight()
obj_ost.intro() obj_ost.intro()
obj_ost.flight() obj_ost.flight()
#ouput: **ouput**:
There are many types of birds. There are many types of birds.
Most of the birds can fly but some cannot. Most of the birds can fly but some cannot.
There are many types of birds. There are many types of birds.
Sparrows can fly. Sparrows can fly.
There are many types of birds. There are many types of birds.
Ostriches cannot fly. Ostriches cannot fly.