In the previous chapter, we talked about superclasses and subclasses.
If a class inherits a method from its superclass, then there is a chance to override the method provided.
You can always override your parent class methods.
One reason for overriding parent's methods is that you may want special or different functionality in your subclass.
print('I am from Parent class')
class Child (Parent):
print('I am from Child class')
p = Parent()
c = Child()
p.echo() # Print 'I am from Parent class'
c.echo() # Print 'I am from Child class'
The following table lists some generic functionality that you can override in your own classes:
|__init__ ( self [,args...] )||Constructor (with any optional arguments)|
|__del__( self )||Destructor, deletes an object|
|__repr__( self )||Evaluatable string representation|
|__str__( self )||Printable string representation|
|__cmp__ ( self, x )||Object comparison|
Abstraction is a process of hiding the implementation details from the user, only the functionality will be provided to the user.
In other words, the user will have the information on what the object does instead of how it does it.
A class which contains at least one abstract method is known as abstract class.
If a class is declared abstract, it cannot be instantiated.
To use an abstract class, you have to inherit it from another class, provide implementations to the abstract methods in it.
If you inherit an abstract class, you have to provide implementations to all the abstract methods in it.
from abc import ABC, abstractmethod
# This will generate TypeError
#obj_1 = Abstract()
obj_2 = Main() # Print 'Abstract class'
obj_2.do_shit() # Print 'shit'