Python_Basics (2)
Python_Basics (2)
In Python, you define a class using the class keyword, and you create an
object by calling the class using the () operator.
Example:
class Car:
def __init__(self, color, model):
self.color = color
self.model = model
def honk(self):
print("Beep!")
In this example, Car is the class, and my_car is an object (an instance of
the Car class.
Constructor:
class MyClass:
def __init__(self, name, age):
self.name = name
self.age = age
In this example, the __init__ method takes two parameters, name and age,
which are used to set the initial state of the object. When the object is
created, the __init__ method is called automatically, and the object's
attributes are set to the passed values.
Abstract method
Abstract classes are defined using the abc (Abstract Base Classes) module.
You need to import the ABC class and the abstractmethod decorator from the
abc module to define an abstract class.
Here's an example:
class AbstractClassExample(ABC):
@abstractmethod
def abstract_method(self):
pass
def concrete_method(self):
print("This is a concrete method")
In this example:
To use an abstract class, you need to inherit from it and implement all the
abstract methods:
class ConcreteClassExample(AbstractClassExample):
def abstract_method(self):
print("Implemented abstract method")
obj = ConcreteClassExample()
obj.abstract_method() # Output: Implemented abstract method
obj.concrete_method() # Output: This is a concrete method
class Animal(ABC):
@abstractmethod
def sound(self):
pass
@abstractmethod
def move(self):
pass
class Dog(Animal):
def sound(self):
return "Bark"
def move(self):
return "Run"
class Bird(Animal):
def sound(self):
return "Chirp"
def move(self):
return "Fly"
# The following will raise an error because you cannot instantiate an abstract
class
# animal = Animal()
bird = Bird()
print(bird.sound()) # Output: Chirp
print(bird.move()) # Output: Fly
Inheritance
The method of inheriting the properties of parent class into a child class
is known as inheritance. It is an OOP concept. The following are the benefits
of inheritance.
Code reusability- we do not have to write the same code, again and
again, we can just inherit the properties we need in a child class.
It represents a real-world relationship between parent class and child
class.
It is transitive in nature. If a child class inherits properties from
a parent class, then all other sub-classes of the child class will
also inherit the properties of the parent class.
Example :
class Parent():
def first(self):
print('first function')
class Child(Parent):
def second(self):
print('second function')
ob = Child()
ob.first()
ob.second()
Types Of Inheritance:
Depending upon the number of child and parent classes involved, there are
four types of inheritance in python.
Single Inheritance
When a child class inherits only a single parent class.
class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print(" this is function 2 ")
ob = Child()
ob.func1()
ob.func2()
Multiple Inheritance
When a child class inherits from more than one parent class.
class Parent:
def func1(self):
print("this is function 1")
class Parent2:
def func2(self):
print("this is function 2")
class Child(Parent , Parent2):
def func3(self):
print("this is function 3")
ob = Child()
ob.func1()
ob.func2()
ob.func3()
Multilevel Inheritance
When a child class becomes a parent class for another child class.
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Child):
def func3("this is function 3")
ob = Child2()
ob.func1()
ob.func2()
ob.func3()
Hierarchical Inheritance
Hierarchical inheritance involves multiple inheritance from the same base or
parent class.
class Parent:
def func1(self):
print("this is function 1")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child2(Parent):
def func3(self):
print("this is function 3")
ob = Child()
ob1 = Child2()
ob.func1()
ob.func2()
Hybrid Inheritance
Hybrid inheritance involves multiple inheritances taking place in a single
program.
class Parent:
def func1(self):
print("this is function one")
class Child(Parent):
def func2(self):
print("this is function 2")
class Child1(Parent):
def func3(self):
print(" this is function 3"):
class Child3(Parent , Child1):
def func4(self):
print(" this is function 4")
ob = Child3()
ob.func1()
Polymorphism
Overloading of operators
Class Polymorphism in Python
Method overriding, also referred to as Run time Polymorphism
Method overloading, also known as Compile time Polymorphism
Magic Methods
In Python, special methods are also called magic methods, or dunder methods.
This latter terminology, dunder, refers to a particular naming convention
that Python uses to name its special methods and attributes. The convention
is to use double leading and trailing underscores in the name at hand, so it
looks like .__method__().
Sample magic methods are:-
Access Modifier
Python uses the ‘_’ symbol to determine the access control for a specific
data member or a member function of a class. Access specifiers in Python
have an important role to play in securing data from unauthorized access and
in preventing it from being exploited.
A Class in Python has three types of access modifiers:
Public Access Modifier
Protected Access Modifier
Private Access Modifier
Sample Programs:-