Abstraction in Oops - Colab
Abstraction in Oops - Colab
ABSTRACTION
Data Abstraction in Python is the process of hiding the real implementation of an application from the user and emphasizing only on usage of
it.
Basically, Abstraction focuses on hiding the internal implementations of a process or method from the user. In this way, the user knows what he
is doing but not how the work is being done.
-----In Python abstraction can be achieved using abstract class and it’s methods
Through the process of abstraction in Python, a programmer can hide all the irrelevant data of an application to reduce complexity and increase
efficiency.
#Syntax:
from abc import ABC
In Python abstraction can be achieved using abstract class and it’s methods.
As a property, abstract classes can have any number of abstract methods coexisting with any number of other methods.
Abstract methods do not contain any implementation. Instead, all the implementations can be defined in the methods of sub-classes that
inherit the abstract class.
We have already discussed the syntax of abstract class above, let’s understand this by taking examples.
https://colab.research.google.com/drive/1HU9H6x1QwpE-syaPbVchV9oUvAzYmFk6#scrollTo=ubZz-QCuvubJ&printMode=true 1/8
6/8/24, 3:18 PM abstraction in oops - Colab
from abc import ABC,abstractmethod
class Polygon(ABC):
#abstract method
@abstractmethod
def sides(self):
pass
#@abstractmethod
def display(self):
pass
class Triangle(Polygon):
def sides(self):
print("triangle as three sides")
def display(self):
print("triangle")
class Rectangle(Polygon):
def sides(self):
print("rectangle has four sides")
def display(self):
print("rectangle")
class Pentagon(Polygon):
def sides(self):
print("pentagon has five sides")
def display(self):
print("pentagon")
class Hexagon(Polygon):
def sides(self):
print("hexagoon has six sides")
def display(self):
print("hexagon")
t=Triangle()
t.sides()
An abstract class can have both a normal method and an abstract method
An abstract class cannot be instantiated, ( we cannot create objects for the abstract class).
s=Square(5)
s.area()
c=Circle(4)
c.area()
print(s.area())
print(c.area())
25
50.24
when you use the @abstractmethod decorator in a base class to define an abstract method, all subclasses must implement that abstract
method.
https://colab.research.google.com/drive/1HU9H6x1QwpE-syaPbVchV9oUvAzYmFk6#scrollTo=ubZz-QCuvubJ&printMode=true 2/8
6/8/24, 3:18 PM abstraction in oops - Colab
class Polygon(ABC):
#abstract method
@abstractmethod
def sides(self):
pass
@abstractmethod
def display(self):
pass
class Triangle(Polygon):
def sides(self):
class Rectangle(Polygon):
def sides(self):
class Pentagon(Polygon):
def sides(self):
class Hexagon(Polygon):
def sides(self):
if choice==1:
t=Triangle()
t.sides()
if choice==2:
r=Rectangle()
r.sides() if choice==3:
p=Pentagon()
p.sides()
if choice ==4:
h=Hexagon()
h.sides()
Here we created two abstract methods in base class but we use only one abstract method in all sub classes so it will show error
https://colab.research.google.com/drive/1HU9H6x1QwpE-syaPbVchV9oUvAzYmFk6#scrollTo=ubZz-QCuvubJ&printMode=true 3/8
6/8/24, 3:18 PM abstraction in oops - Colab
from abc import ABC,abstractmethod
class Llgm(ABC):
def __init__(self,choice):
self.choice=choice
@abstractmethod
def area(self):
pass
class Square(Llgm):
def __init__(self,length,choice):
super().__init__(choice)
self.length=length
def area(self):
return self.length * self.length
class Circle(Llgm):
def __init__(self,radius,choice):
super().__init__(choice)
self.radius=radius
def area(self):
return 3.14 * self.radius * self.radius
s=Square(5,choice)
s.area()
print(s.area())
if choice ==2:
c=Circle(4,choice)
c.area()
print(c.area())
An abstract class cannot be instantiated directly. It can only be used as a base class for other classes.
An abstract method is a method declared in an abstract class that has no implementation in the base class. Subclasses must
provide an implementation for these methods.
In Python, you use the ABC (Abstract Base Class) module and the @abstractmethod decorator from the abc module to define
abstract classes and methods.
A class that contains one or more abstract methods must be declared as an abstract class by inheriting from ABC .
3. Subclass Implementation:
Subclasses of an abstract class must implement all abstract methods declared in the base class.
If a subclass does not implement all abstract methods, it will also be considered abstract and cannot be instantiated.
4. Instantiation:
5. Partial Implementation:
Abstract classes can have concrete (non-abstract) methods with implementations. These methods can be used by subclasses as is
or can be overridden.
6. Code Reusability:
Abstract classes promote code reusability by allowing you to define common behavior that can be shared by multiple subclasses.
In this example:
Shape is an abstract class with two abstract methods: area and perimeter .
Rectangle and Circle are subclasses of Shape and provide implementations for the area and perimeter methods.
You cannot create an instance of Shape directly, but you can create instances of Rectangle and Circle .
These rules ensure that abstraction is properly implemented, promoting a clear and maintainable code structure.
https://colab.research.google.com/drive/1HU9H6x1QwpE-syaPbVchV9oUvAzYmFk6#scrollTo=ubZz-QCuvubJ&printMode=true 4/8
6/8/24, 3:18 PM abstraction in oops - Colab
from abc import ABC, abstractmethod
@abstractmethod
def area(self):
pass
@abstractmethod
def perimeter(self):
pass
def area(self):
return self.width * self.height
def perimeter(self):
return 2 * (self.width + self.height)
def area(self):
return 3.14 * self.radius * self.radius
def perimeter(self):
return 2 * 3.14 * self.radius
circle = Circle(7)
https://colab.research.google.com/drive/1HU9H6x1QwpE-syaPbVchV9oUvAzYmFk6#scrollTo=ubZz-QCuvubJ&printMode=true 5/8
6/8/24, 3:18 PM abstraction in oops - Colab
# An abstract class can have both a normal method and an abstract method
# An abstract class cannot be instantiated, ( we cannot create objects for the abstract class).
class Ope:
def __init__(self,a,b):
self.a=a
self.b=b
@abstractmethod
def add(self):
pass
@abstractmethod
def mul(self):
pass
def discription(self):
print('addition or mul')
class Two(Ope):
def add(self):
print(self.a+self.b)
def mul(self):
print(self.a*self.b)
class Three(Ope):
def __init__(self,a,b,c):
super().__init__(a,b)
self.c=c
def add(self):
print(self.a+self.b+self.c)
def mul(self):
print(self.a*self.b*self.c)
t=Two(1,2)
t.add()
t.mul()
t.discription()
r=Three(1,2,3)
r.add()
r.mul()
r.discription()
3
2
addition or mul
6
6
addition or mul
class ATM(ABC):
def __init__(self,amount):
self.amount=amount
print("choose your options")
print("1.withdraw 2. balance enquire 3.pin change")
@abstractmethod
def withdraw(self):
pass
@abstractmethod
def balenq(self):
pass
@abstractmethod
def pinchange(self):
pass
class Ope(ATM):
def __init__(self,amount,pin):
super().__init__(amount)
self.pin=pin
print(self.pin)
lf i i t(i t(' t th i '))
https://colab.research.google.com/drive/1HU9H6x1QwpE-syaPbVchV9oUvAzYmFk6#scrollTo=ubZz-QCuvubJ&printMode=true 6/8
6/8/24, 3:18 PM abstraction in oops - Colab
self.pin=int(input('enter the pin'))
print(self.pin)
def withdraw(self):
if self.pin==1234:
print("valid")
cash=int(input("enter the amount to withdraw"))
print("please collect your cash",cash)
balance=self.amount-cash
print("remaining amount was",balance)
else:
print("invalid pin")
raise SystemExit
def balenq(self):
print("remaining amount was",balance)
def pinchange(self):
if self.pin ==1234:
self.pin=4567
print("new pin",self.pin)
o=Ope(10000,None)
option=int(input("enter the option"))
if option ==1:
o.withdraw()
if option ==2:
o.balenq()
if option==3:
o.pinchange()
class Square(Polygons):
def __init__(self,side):
self.side=side
def sides(self):
print('square ha 4 sides')
def display(self):
print('it is a square')
def area(self):
res=self.side*self.side
print(res)
s=Square(5)
s.sides()
s.display()
s.area()
square ha 4 sides
it is a square
25
GENERATORES
https://colab.research.google.com/drive/1HU9H6x1QwpE-syaPbVchV9oUvAzYmFk6#scrollTo=ubZz-QCuvubJ&printMode=true 7/8
6/8/24, 3:18 PM abstraction in oops - Colab
def gen():
for i in range(0,11):
yield i
https://colab.research.google.com/drive/1HU9H6x1QwpE-syaPbVchV9oUvAzYmFk6#scrollTo=ubZz-QCuvubJ&printMode=true 8/8