Tue Aug 03 2021

Inheritance

File Name: inheritance.py

#!/usr/bin/evn python

# Define a super class as 'toDecimal'
class toDecimal:
    def setData(self, data):
        self.binary = data
        self.decimal = int(self.binary, 2)
    
    def disDec(self):
        print("Decimal value:",self.decimal)
        
# Define a sub class as 'toOctal' and inherit super class 'toDecimal' 
class toOctal(toDecimal):
    def disOct(self):
        print("Octal value:",oct(self.decimal))

# Define a sub class as 'toHexl' and inherit super class 'toDecimal'         
class toHex(toDecimal):
    def disHex(self):
        print("Hex value:",hex(self.decimal))
        
# 'num1' is the object of the class 'toDecimal'
num1 = toDecimal()
# Calling member function 'setData() by passing value'
num1.setData("1101")
# Calling member function 'disDec()'
num1.disDec()

# 'num2' is the object of the class 'toOctal'
num2 = toOctal()
# Calling super class member function 'setData() by passing value'
num2.setData("1011")
# Calling super class member function 'disDec()'
num2.disDec()
# Calling member function 'disOct()'
num2.disOct()

# 'num3' is the object of the class 'toHex'
num3 = toHex()
# Calling super class member function 'setData() by passing value'
num3.setData("11010")
# Calling super class member function 'disDec()'
num3.disDec()
# Calling member function 'disHex()'
num3.disHex()



#***** Output *****
# Decimal value: 13
# Decimal value: 11
# Octal value: 0o13
# Decimal value: 26
# Hex value: 0x1a

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.