Abstract Method
Abstract Method
Code 1:
Python3
:
# Python program showing
# abstract base class work
class Polygon(ABC):
@abstractmethod
def noofsides(self):
pass
class Triangle(Polygon):
class Pentagon(Polygon):
class Hexagon(Polygon):
class Quadrilateral(Polygon):
K = Quadrilateral()
K.noofsides()
R = Pentagon()
R.noofsides()
:
K = Hexagon()
K.noofsides()
Output:
I have 3 sides
I have 4 sides
I have 5 sides
I have 6 sides
Code 2:
vide-so<ware.at
OPEN
Python3
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()
Output:
I can crawl
:
I can bark
I can roar
Implementation Through Subclassing :
By subclassing directly from the base, we can avoid the need to register the class
explicitly. In this case, the Python class management is used to recognize
PluginImplementation as implementing the abstract PluginBase.
Python3
import abc
class parent:
def geeks(self):
pass
class child(parent):
def geeks(self):
print("child class")
# Driver code
print( issubclass(child, parent))
print( isinstance(child(), parent))
Output:
True
True
:
A side-e!ect of using direct subclassing is, it is possible to find all the
implementations of your plugin by asking the base class for the list of known
classes derived from it.
Concrete Methods in Abstract Base Classes :
Concrete classes contain only concrete (normal)methods whereas abstract classes
may contain both concrete methods and abstract methods. The concrete class
provides an implementation of abstract methods, the abstract base class can also
provide an implementation by invoking the methods via super().
Let look over the example to invoke the method using super():
Python3
import abc
from abc import ABC, abstractmethod
class R(ABC):
def rk(self):
print("Abstract Base Class")
class K(R):
def rk(self):
super().rk()
print("subclass ")
# Driver code
r = K()
r.rk()
Output:
:
Abstract Base Class
subclass
In the above program, we can invoke the methods in abstract classes by using
super().
Abstract Properties :
Abstract classes include attributes in addition to methods, you can require the
attributes in concrete classes by defining them with @abstractproperty.
:
Python3
import abc
from abc import ABC, abstractmethod
class parent(ABC):
@abc.abstractproperty
def geeks(self):
return "parent class"
class child(parent):
@property
def geeks(self):
return "child class"
try:
r =parent()
print( r.geeks)
except Exception as err:
print (err)
r = child()
print (r.geeks)
Output:
child class
:
In the above example, the Base class cannot be instantiated because it has only an
abstract version of the property getter method.
Abstract Class Instantiation :
Abstract classes are incomplete because they have methods that have nobody. If
python allows creating an object for abstract classes then using that object if
anyone calls the abstract method, but there is no actual implementation to invoke.
So we use an abstract class as a template and according to the need, we extend it
and build on it before we can use it. Due to the fact, an abstract class is not a
concrete class, it cannot be instantiated. When we create an object for the abstract
class it raises an error.
:
Python3
class Animal(ABC):
@abstractmethod
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")
c=Animal()
Output:
c=Animal()
TypeError: Can't instantiate abstract class Animal with abstract methods move
:
Recommended Articles
1. Abstract Factory Method - Python Design Patterns
2. Abstract Base Class (abc) in Python
3. Implementing Web Crawler using Abstract Factory Design Pattern in Python
4. PyQt5 QCalendarWidget - Setting Border to the Abstract View
5. PyQt5 QCalendarWidget - Background Color to the Abstract View
6. How to create Abstract Model Class in Django?
7. Data Classes in Python | An Introduction
8. Data Classes in Python | Set 2 (Decorator Parameters)
9. Data Classes in Python | Set 3 (dataclass fields)
10. Data Classes in Python | Set 5 (post-init)
11. Data Classes in Python | Set 6 (interconversion to and from other datatypes)
12. How to Dynamically Load Modules or Classes in Python
13. Create Classes Dynamically in Python
14. The Ultimate Guide to Data Classes in Python 3.7
15. Data Classes in Python | Set 4 (Inheritance)
16. Python Classes and Objects
17. How to Handle Imbalanced Classes in Machine Learning
18. Create a Scatter Plot using Sepal length and Petal_width to Separate the Species
Classes Using scikit-learn
19. Important di!erences between Python 2.x and Python 3.x with examples
20. Python | Merge Python key values to list
21. Reading Python File-Like Objects from C | Python
22. Python | Add Logging to a Python Script
23. Python | Add Logging to Python Libraries
:
24. JavaScript vs Python : Can Python Overtop JavaScript by 2020?
25. Python | Visualizing O(n) using Python
Company
About Us
Careers
In Media
Contact Us
Privacy Policy
Copyright Policy
Advertise with us
:
Learn
DSA
Algorithms
Data Structures
SDE Cheat Sheet
Machine Learning
CS Subjects
Video Tutorials
Courses
NEWS
Top News
Technology
Work & Career
Business
Finance
Lifestyle
Knowledge
:
Knowledge
Languages
Python
Java
CPP
Golang
C#
SQL
Kotlin
Web Development
Web Tutorials
Django Tutorial
HTML
JavaScript
Bootstrap
ReactJs
NodeJs
Contribute
Write an Article
Improve an Article
Pick Topics to Write
Write Interview Experience
Internships
Video Internship
:
@geeksforgeeks, Some rights reserved
: