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