English

How do we implement abstract method in Python. Support your answer with an example.

Advertisements
Advertisements

Question

How do we implement abstract method in Python. Support your answer with an example.

Code Writing
Advertisements

Solution

An abstract method may be declared in a parent class by raising NotImplementedError. A derived class implements it by overriding the method.

class Circle(object):
    def get_radius(self):
        raise NotImplementedError

class Circle1(Circle):
    def __init__(self, radius):
        self.radius = radius

    def get_radius(self):
        return self.radius

c = Circle1(10)
print(c.get_radius())

Output

10
shaalaa.com
  Is there an error in this question or solution?
Chapter 4: Inheritance - EXERCISE [Page 78]

APPEARS IN

CBSE Computer Science with Python [English] Class 12
Chapter 4 Inheritance
EXERCISE | Q 13. | Page 78
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×