Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python abstract method

# Python program showing
# abstract base class work
 
from abc import ABC, abstractmethod
 
class Polygon(ABC):
 
    @abstractmethod
    def noofsides(self):
        pass
 
class Triangle(Polygon):
 
    # overriding abstract method
    def noofsides(self):
        print("I have 3 sides")
 
class Pentagon(Polygon):
 
    # overriding abstract method
    def noofsides(self):
        print("I have 5 sides")
 
class Hexagon(Polygon):
 
    # overriding abstract method
    def noofsides(self):
        print("I have 6 sides")
 
class Quadrilateral(Polygon):
 
    # overriding abstract method
    def noofsides(self):
        print("I have 4 sides")
 
# Driver code
R = Triangle()
R.noofsides()
 
K = Quadrilateral()
K.noofsides()
 
R = Pentagon()
R.noofsides()
 
K = Hexagon()
K.noofsides()
Comment

abstract class python

An abstract class exists only so that other "concrete" classes can inherit from the abstract class.
Comment

abstract class in python

class Circle(Shape):
    def __init__(self):
        super().__init__("circle")
 
    @property
    def name(self):
        return self.shape_name
 	def draw(self):    
        print("Drawing a Circle")
Comment

Abstract Classes in Python

# Python program showing
# abstract base class work
 
from abc import ABC, abstractmethod
class Animal(ABC):
 
    def move(self):
        pass
 
class Human(Animal):
 
    def move(self):
        print("I can walk and run")
 
class Snake(Animal):
 
    def move(self):
        print("I can crawl")
 
class Dog(Animal):
 
    def move(self):
        print("I can bark")
 
class Lion(Animal):
 
    def move(self):
        print("I can roar")
         
# Driver code
R = Human()
R.move()
 
K = Snake()
K.move()
 
R = Dog()
R.move()
 
K = Lion()
K.move()
Comment

PREVIOUS NEXT
Code Example
Python :: print list vertically python 
Python :: create a pandas dataframe 
Python :: transpose of a matrix in python using loop 
Python :: format python decimal 
Python :: docker python 3.11 
Python :: dataframe 
Python :: append multiple elements python 
Python :: json.dump 
Python :: pygame python 
Python :: for i in range python 
Python :: bounding box in matplotlib 
Python :: loop python 
Python :: how to check if a value is nan in python 
Python :: if statements python 
Python :: python chatbot error 
Python :: * pattern program in python 
Python :: false python 
Python :: python value error 
Python :: match in python 
Python :: pip path windows 10 
Python :: how to reverse string in python 
Python :: how to get a user input in python 
Python :: initialize 2d array of zeros python 
Python :: how to sleep() in python 
Python :: text detection from image using opencv python 
Python :: is python idle an ide 
Python :: how to check if a string value is nan in python 
Python :: Generation of Random Numbers in python 
Python :: add in python 
Python :: pong code python 
ADD CONTENT
Topic
Content
Source link
Name
8+8 =