0% found this document useful (0 votes)
5 views1 page

Understanding Abstraction in Python

Abstraction is a fundamental principle of object-oriented programming (OOP) that conceals implementation details while exposing relevant information to users. In Python, abstraction is achieved using Abstract Base Classes (ABC), which can define abstract methods that must be implemented by derived classes. The document also includes examples of creating abstract classes and concrete classes, such as Vehicle, Car, Bike, Shape, Rectangle, and Circle, along with their respective abstract methods and functionalities.

Uploaded by

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

Understanding Abstraction in Python

Abstraction is a fundamental principle of object-oriented programming (OOP) that conceals implementation details while exposing relevant information to users. In Python, abstraction is achieved using Abstract Base Classes (ABC), which can define abstract methods that must be implemented by derived classes. The document also includes examples of creating abstract classes and concrete classes, such as Vehicle, Car, Bike, Shape, Rectangle, and Circle, along with their respective abstract methods and functionalities.

Uploaded by

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

abstraction is one the pillar of opps where we hide the implementation of

functionality and showing only the relevent details to users.

feature of abstraction :-
flexible program
increase the security
data hiding
syntax :- in pyython we acheieve abstraction using ABC (abstract base class)
module
syntax of abstract class:- from abc import ABC
class A (ABC):
# abstract class
def __ int __(self):
print("this is abstract class ")

# abstract method :- a method which doesnot have any body is known as abstract
method.

@abstract nethod
def abcd (self):(
pass

here abstract method is an anotation or decorator which tells that given function
is an abstract function .
[Link] we define a abstract method inside a normal class?
Ans :-no.
we can define a concrete method inside a abstract class .

[Link]:
Create an abstract class Vehicle that has the following:
1. An abstract method start_engine().
2. An abstract method stop_engine().
3. A method vehicle_type() that returns the type of vehicle.

Then, create two concrete classes Car and Bike that inherit from Vehicle and
implement the abstract methods.
Finally, create instances of Car and Bike and demonstrate their functionality.

Problem:
Design an abstract class Shape with the following:
1. An abstract method area() that calculates the area of the shape.
2. An abstract method perimeter() that calculates the perimeter of the shape.

Then, implement two concrete classes:


1. Rectangle with attributes length and width.
2. Circle with attribute radius.

Both classes should override the abstract methods.


Finally, write a function shape_info(shape: Shape) that accepts a Shape object,
prints its area and perimeter, and raises an error if the object is not derived
from the Shape class.#abstract

You might also like