Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python code to demonstrate inheritance

# Python code to demonstrate how parent constructors
# are called.
  
# parent class
class Person( object ):    
  
        # __init__ is known as the constructor         
        def __init__(self, name, idnumber):   
                self.name = name
                self.idnumber = idnumber
        def display(self):
                print(self.name)
                print(self.idnumber)
  
# child class
class Employee( Person ):           
        def __init__(self, name, idnumber, salary, post):
                self.salary = salary
                self.post = post
  
                # invoking the __init__ of the parent class 
                Person.__init__(self, name, idnumber) 
  
                  
# creation of an object variable or an instance
a = Employee('Rahul', 886012, 200000, "Intern")    
  
# calling a function of the class Person using its instance
a.display()
Comment

python inheritance

class Base():
    """ My base class """

    __nb_instances = 0

    def __init__(self):
        Base.__nb_instances += 1
        self.id = Base.__nb_instances

class User(Base):
    """ My User class """

    def __init__(self):
        super().__init__()
        self.id += 99

u = User()
print(u.id)
Comment

inheritance in python 3 example

class Robot:
    
    def __init__(self, name):
        self.name = name
        
    def say_hi(self):
        print("Hi, I am " + self.name)
        
class PhysicianRobot(Robot):

    def say_hi(self):
        print("Everything will be okay! ") 
        print(self.name + " takes care of you!")

y = PhysicianRobot("James")
y.say_hi()
Comment

How to inheritance in Python

# Python code to demonstrate how parent constructors
# are called.
 
# parent class
class Person(object):
 
    # __init__ is known as the constructor
    def __init__(self, name, idnumber):
        self.name = name
        self.idnumber = idnumber
 
    def display(self):
        print(self.name)
        print(self.idnumber)
 
# child class
 
 
class Employee(Person):
    def __init__(self, name, idnumber, salary, post):
        self.salary = salary
        self.post = post
 
        # invoking the __init__ of the parent class
        Person.__init__(self, name, idnumber)
 
 
# creation of an object variable or an instance
a = Employee('Rahul', 886012, 200000, "Intern")
 
# calling a function of the class Person using its instance
a.display()
Comment

Python Inheritance

class Student(Person):
  def __init__(self, fname, lname, year):
    super().__init__(fname, lname)
    self.graduationyear = year
 

x = Student("Michael", "Smith", 2020)
Comment

inheritance in python 3 example

class Robot:
    
    def __init__(self, name):
        self.name = name
        
    def say_hi(self):
        print("Hi, I am " + self.name)
        
class PhysicianRobot(Robot):
    pass

x = Robot("Marvin")
y = PhysicianRobot("James")

print(x, type(x))
print(y, type(y))

y.say_hi()
Comment

python inheritance

class Coordinates:
    def __init__(self, x, y):
        self.__x = x #variables that start with double _ are private and only can be accessed by method
        self.__y = y
    def x(self):
        return self.__x
    def y(self):
        return self.__y
    def position(self):
        return (self.__x, self.__y)

class Square(Coordinates):
    def __init__(self, x, y, w, h):
        super().__init__(x, y)
        self.__w = w
        self.__h = h
    def w(self):
        return self.__w
    def h(self):
        return self.__h
    def area(self):
        return self.__w * self.__h

class Cube(Square):
    def __init__(self, x, y, w, h, d):
        super().__init__(x,y,w,h)
        self.__d = d
    def d(self):
        return self.__d
    def area(self): #overwrites square area function
        return 2 * (self.__w * self.__h + self.__d * self.__h + self.__w * self.__d)
    def volume(self):
        return self.__w * self.__h * self.__d
Comment

A python program to demonstrate inheritance

# A Python program to demonstrate inheritance
  
# Base or Super class. Note object in bracket.
# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is
# equivalent to "class Person(object)"
class Person(object):
      
    # Constructor
    def __init__(self, name):
        self.name = name
  
    # To get name
    def getName(self):
        return self.name
  
    # To check if this person is an employee
    def isEmployee(self):
        return False
  
  
# Inherited or Subclass (Note Person in bracket)
class Employee(Person):
  
    # Here we return true
    def isEmployee(self):
        return True
  
# Driver code
emp = Person("Geek1")  # An Object of Person
print(emp.getName(), emp.isEmployee())
  
emp = Employee("Geek2") # An Object of Employee
print(emp.getName(), emp.isEmployee())
Comment

A python program to demonstrate inheritance

# A Python program to demonstrate inheritance
  
# Base or Super class. Note object in bracket.
# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is
# equivalent to "class Person(object)"
class Person(object):
      
    # Constructor
    def __init__(self, name):
        self.name = name
  
    # To get name
    def getName(self):
        return self.name
  
    # To check if this person is an employee
    def isEmployee(self):
        return False
  
  
# Inherited or Subclass (Note Person in bracket)
class Employee(Person):
  
    # Here we return true
    def isEmployee(self):
        return True
  
# Driver code
emp = Person("Geek1")  # An Object of Person
print(emp.getName(), emp.isEmployee())
  
emp = Employee("Geek2") # An Object of Employee
print(emp.getName(), emp.isEmployee())
Comment

Python Inheritance

class BaseClass:
  Body of base class
class DerivedClass(BaseClass):
  Body of derived class
Comment

Inheritance in Python

class Polygon:
    def __init__(self, no_of_sides):
        self.n = no_of_sides
        self.sides = [0 for i in range(no_of_sides)]

    def inputSides(self):
        self.sides = [float(input("Enter side "+str(i+1)+" : ")) for i in range(self.n)]

    def dispSides(self):
        for i in range(self.n):
            print("Side",i+1,"is",self.sides[i])
Comment

inheritance in python 3 example

<__main__.Robot object at 0x7fd0080b3ba8> <class '__main__.Robot'>
<__main__.PhysicianRobot object at 0x7fd0080b3b70> <class '__main__.PhysicianRobot'>
Hi, I am James
Comment

A python program to demonstrate inheritance

# A Python program to demonstrate inheritance
  
# Base or Super class. Note object in bracket.
# (Generally, object is made ancestor of all classes)
# In Python 3.x "class Person" is
# equivalent to "class Person(object)"
class Person(object):
      
    # Constructor
    def __init__(self, name):
        self.name = name
  
    # To get name
    def getName(self):
        return self.name
  
    # To check if this person is an employee
    def isEmployee(self):
        return False
  
  
# Inherited or Subclass (Note Person in bracket)
class Employee(Person):
  
    # Here we return true
    def isEmployee(self):
        return True
  
# Driver code
emp = Person("Geek1")  # An Object of Person
print(emp.getName(), emp.isEmployee())
  
emp = Employee("Geek2") # An Object of Employee
print(emp.getName(), emp.isEmployee())
Comment

inheritance in Python

from Chef import Chef

class ChineseChef(Chef):

	def make_salad():
		print("Chinese chef makes a salad")
Comment

PREVIOUS NEXT
Code Example
Python :: pandas groupby 
Python :: plotly create plot 
Python :: panda loc conditional 
Python :: create an array filled with 0 
Python :: django serializer get image list 
Python :: python counting up and down 
Python :: looping over dictionary python 
Python :: float error python 
Python :: print value of array python 
Python :: with function python 
Python :: python immutable dataclass 
Python :: normal discord.py codes 
Python :: how to make python script run forever 
Python :: fill_between matplotlib 
Python :: python check if value in string 
Python :: how to print values without space in python 
Python :: pandas create dataframe from multiple dictionaries 
Python :: python get bits from byte 
Python :: decorators in python 
Python :: dataframe rolling window 
Python :: reshape 
Python :: tic tac toe pygame 
Python :: python change label text 
Python :: var colors python 
Python :: how to add legend on side of the chart python 
Python :: series object has no attribute split 
Python :: numpy dataframe 
Python :: dict comprehension python 
Python :: how to slice few rows in pandas 
Python :: sorted python 
ADD CONTENT
Topic
Content
Source link
Name
7+5 =