My abstract method is not working, why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5
23rd Apr 2022, 1:02 AM
Lenoname
17 Answers
+ 12
Abstract methods do not get implement in the abstract class. All subclasses must implement them. That is what abstract means.
23rd Apr 2022, 1:13 AM
John Wells
John Wells - avatar
+ 5
cat.info() will call the pet.info() method if it is not abstract. Abstract is meant for something like speak. They all do it in their own way. Not for some common like info.
23rd Apr 2022, 1:55 AM
John Wells
John Wells - avatar
+ 4
No remove the @abstractmethod from pet's info method and all three get it automatically. If you want to force each class to implement a speak method, that is when you use it.
23rd Apr 2022, 2:20 AM
John Wells
John Wells - avatar
+ 4
But the info method does not.
23rd Apr 2022, 2:54 AM
John Wells
John Wells - avatar
+ 3
Yes the class stays abstract
23rd Apr 2022, 2:55 AM
John Wells
John Wells - avatar
+ 3
Abstracting the class only means you can not create an instance of it. Everything non abstract in it could be used without issue by the subclasses. Any abstract method in it must be overrided in the subclasses meaning they have their own uniqueness.
23rd Apr 2022, 2:58 AM
John Wells
John Wells - avatar
+ 2
The whole purpose of having a share parent class is to hold everything they have in common.
23rd Apr 2022, 1:57 AM
John Wells
John Wells - avatar
+ 2
Lenoname missing colon from abc import ABC, abstractmethod class A(ABC) : def __init__(self, name, lastname) : # colon missing self.name = name self.lastname = lastname
24th Apr 2022, 2:02 AM
John Wells
John Wells - avatar
+ 1
John Wells how do i implement them then? The idea is to make cat bird hamster classes to inherit pet class and in the end call the info() method through other subclasses and not directly from pet class, by the way even if i delete info() method it wont work
23rd Apr 2022, 1:31 AM
Lenoname
+ 1
How do i make the pet class abstract without making the info() method abstract?
23rd Apr 2022, 2:56 AM
Lenoname
+ 1
you can't call an abstract method from the class you need a subclass
24th Apr 2022, 10:30 PM
Pls Dont Block Me
0
So i have to write info() method in all 3 subclasses?
23rd Apr 2022, 1:57 AM
Lenoname
0
John Wells Will the pet class then stay abstract if i remove the @abstractmethod?
23rd Apr 2022, 2:37 AM
Lenoname
0
The pet class must stay abstract
23rd Apr 2022, 2:39 AM
Lenoname
0
Ok how do i for example make this class abstract?: class A : def __init__(self, name, lastname) self.name = name self.lastname = lastname
23rd Apr 2022, 3:08 AM
Lenoname
0
from abc import ABC, abstractmethod class A(ABC) : def __init__(self, name, lastname) self.name = name self.lastname = lastname Is this enough?
23rd Apr 2022, 3:10 AM
Lenoname
- 1
Anyone answer this question How can this answer output Print (x*10) 👈🙏
23rd Apr 2022, 5:20 PM
GøĐ GïfŤ
GøĐ GïfŤ - avatar