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

Instantiation Issues with Abstract Classes

The document discusses the instantiation of abstract classes in Python, highlighting that if a class contains an abstract method and extends the ABC class, instantiation is not possible. It provides examples demonstrating that instantiation can occur if the abstract class is not extended from ABC. The conclusion emphasizes that abstract methods must be implemented in child classes to allow instantiation.

Uploaded by

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

Instantiation Issues with Abstract Classes

The document discusses the instantiation of abstract classes in Python, highlighting that if a class contains an abstract method and extends the ABC class, instantiation is not possible. It provides examples demonstrating that instantiation can occur if the abstract class is not extended from ABC. The conclusion emphasizes that abstract methods must be implemented in child classes to allow instantiation.

Uploaded by

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

TypeError: Can't instantiate abstract class Test with abstract methods m1

Case-4:

1) from abc import *


2) class Test:
3) @abstractmethod
4) def m1(self):
5) pass
6)
7) t=Test()

We can create object even class contains abstract method b'z we are not extending ABC class.

Case-5:

1) from abc import *


2) class Test:
3) @abstractmethod
4) def m1(self):
5) print('Hello')
6)
7) t=Test()
8) t.m1()

Output: Hello

Conclusion: If a class contains atleast one abstract method and if we are extending ABC class then
instantiation is not possible.

"abstract class with abstract method instantiation is not possible"

Parent class abstract methods should be implemented in the child classes. otherwise we cannot
instantiate child [Link] we are not creating child class object then we won't get any error.

Case-1:

1) from abc import *


2) class Vehicle(ABC):
3) @abstractmethod
4) def noofwheels(self):
5) pass
6)
7) class Bus(Vehicle): pass

It is valid b'z we are not creating Child class object

nd
DURGASOFT, # 202, 2 Floor, HUDA Maitrivanam, Ameerpet, Hyderabad - 500038,
3  040 – 64 51 27 86, 80 96 96 96 96, 92 46 21 21 43 | [Link]

You might also like