0% found this document useful (0 votes)
16 views8 pages

Abstraction in Oops - Colab

Abstraction in oops

Uploaded by

narendersree2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views8 pages

Abstraction in Oops - Colab

Abstraction in oops

Uploaded by

narendersree2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

6/8/24, 3:18 PM abstraction in oops - Colab

ABSTRACTION

Data Abstraction in Python:

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

Why Do We Need Abstraction?

Through the process of abstraction in Python, a programmer can hide all the irrelevant data of an application to reduce complexity and increase
efficiency.

Double-click (or enter) to edit

#Syntax:
from abc import ABC

In Python abstraction can be achieved using abstract class and it’s methods.

Abstract Class in Python

A class containing one or more abstract methods is called an abstract class.

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.

"@abstractmethod" is not an allowed annotation - allowed values include


[@param, @title, @markdown]. edit

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()

triangle as three 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).

from abc import ABC,abstractmethod


class Llgm(ABC):
@abstractmethod
def area(self):
pass
class Square(Llgm):
def __init__(self,length):
self.length=length
def area(self):
return self.length * self.length
class Circle(Llgm):
def __init__(self,radius):
self.radius=radius
def area(self):
return 3.14 * self.radius * self.radius

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.

from abc import ABC,abstractmethod

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):

print("triangle as three sides")

class Rectangle(Polygon):

def sides(self):

print("rectangle has four sides")

class Pentagon(Polygon):

def sides(self):

print("pentagon has five sides")

class Hexagon(Polygon):

def sides(self):

print("hexagoon has six sides")

choice=int(input('enter the chooice'))

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

choice=int(input('enter the choice'))


if choice==1:

s=Square(5,choice)
s.area()
print(s.area())
if choice ==2:
c=Circle(4,choice)
c.area()
print(c.area())

enter the choice1


25

Rules for abstraction

1. Abstract Classes and Methods:

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.

2. Using the ABC and abstractmethod Decorators:

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:

You cannot create an instance of an abstract class directly.


You can create instances of subclasses that provide implementations for all abstract methods.

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

keyboard_arrow_down Define an abstract class


class Shape(ABC):

@abstractmethod
def area(self):
pass

@abstractmethod
def perimeter(self):
pass

Subclass must implement all abstract methods


class Rectangle(Shape):

def __init__(self, width, height):


self.width = width
self.height = height

def area(self):
return self.width * self.height

def perimeter(self):
return 2 * (self.width + self.height)

Subclass must implement all abstract methods


class Circle(Shape):

def __init__(self, radius):


self.radius = radius

def area(self):
return 3.14 * self.radius * self.radius

def perimeter(self):
return 2 * 3.14 * self.radius

Create instances of the subclasses


rectangle = Rectangle(5, 10)

print("Rectangle Area:", rectangle.area())

print("Rectangle Perimeter:", rectangle.perimeter())

circle = Circle(7)

print("Circle Area:", circle.area())

print("Circle Perimeter:", circle.perimeter())

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

from abc import ABC, abstractmethod


print("welcome to our bank")
card=int(input("insert the card"))
if card!= 1:
print("card is valid")
raise SystemExit

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()

welcome to our bank


insert the card1
choose your options
1.withdraw 2. balance enquire 3.pin change
None
enter the pin1234
1234
enter the option1
valid
enter the amount to withdraw10000
please collect your cash 10000
remaining amount was 0

from abc import ABC,abstractmethod


class Polygons(ABC):
@abstractmethod
def sides(self):
pass
def display(self):
pass

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

You might also like