Abstract Classes in Python
Abstract Class in Python
An Abstract Class is a class that is specifically defined to lay a foundation for other classes that exhibit a common behaviour or similar characteristics. It is primarily used only as a base class for inheritance.
An Abstract Class is an incomplete class, users are not allowed to create its objects. To use such a class, programmers must derive it keeping in mind that they would only be either using or overriding the features specified in that class.
Example:
class Fruit: def taste(self): raise NotImplementError() def rich_in(self): raise NotImplementError() def colour(self): raise NotImplementError() class Mango(Fruit): def taste(self): return "Sweet" def rich_in(self): return "Vitamin A" def colour(self): return "Yellow" class Orange(Fruit): def taste(self): return "Sour" def rich_in(self): return "Vitamin C" def colour(self): return "Orange" Alphanso=Mango() print(Alphanso.taste(), Alphanso.rich_in(), Alphanso.colour()) Org=Orange() print(Org.taste(), Org.rich_in(), Org.colour())
Output: