Last active
May 24, 2018 10:56
-
-
Save sudhanshuptl/35a5023eb1a00d67425413f48d82f012 to your computer and use it in GitHub Desktop.
Object Oriented programming Python , Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Destructer delete object by itself when its reference is 0 | |
from abc import ABC, ABCMeta, abstractclassmethod | |
''' | |
Abstract classes are classes that contain one or more abstract methods. An abstract method is a method that is declared, | |
but contains no implementation. Abstract classes may not be instantiated, and require subclasses to provide | |
implementations for the abstract methods. | |
''' | |
# Class That can't be instantiated | |
class AbstractClassDemo(ABC): | |
@abstractclassmethod | |
def skills(self): | |
print('Hello') | |
#abs = AbstractClassDemo() | |
## It though error as Abstract class can't be instantiated | |
class Base(AbstractClassDemo): | |
def __init__(self): | |
self.name = 'Sudhanshu Patel' | |
def skills(self): | |
print('I am in Base Class') | |
def __del__(self): | |
class_name = self.__class__.__name__ | |
print( class_name, 'Destroyed Successfully') | |
obj = Base() | |
obj.skills() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Priority in Multiple Inheritance | |
# Skills function call is based on priority that is decided by order in which class is inherited | |
class Father: | |
def skills(self): | |
print('Programming, Movies') | |
class Mother: | |
def skills(self): | |
print('Cooking, Dance ') | |
class Child1(Mother, Father): | |
# Order of inheritance provide priority in case of ambiguity | |
pass | |
class Child2(Father, Mother): | |
pass | |
print('\nSkilles of child 1___') | |
c1 = Child1() | |
c1.skills() | |
#print : Cooking, Dance | |
print('\n\nSkilles of child 2___') | |
c2 = Child2() | |
c2.skills() | |
#print : Programming, Movies |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Father: | |
def skills(self): | |
print('Programming, Movies') | |
class Mother: | |
def skills(self): | |
print('Cooking, Dance ') | |
class Child1(Mother, Father): | |
def skills(self): | |
super().skills() | |
# It calls skill function from mother class as mother is on priority | |
# If not found then lookup for Father class | |
# To explicitly call skill from father class we can use like this | |
Father.skills(self) | |
print('Sports, Technology') | |
print('\nSkilles of child ___') | |
c1 = Child1() | |
c1.skills() | |
print('__**Explicitly when we want to call skill function from Father class when skill function overriden in child**__') | |
Father.skills(c1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment