Inheritance is a fundamental concept in object-oriented programming (OOP). It allows you to create a new class (child or subclass) that inherits attributes and methods from an existing class (parent or superclass). This helps in code reuse and makes it easier to maintain and extend programs.
Suppose we want to model animals:
class Animal:
def speak(self):
print("The animal makes a sound.")
class Dog(Animal): # Dog inherits from Animal
def speak(self):
print("Woof!")
# Usage
a = Animal()
a.speak() # Output: The animal makes a sound.
d = Dog()
d.speak() # Output: Woof!
Explanation:
Animal is the parent class with a method speak.Dog is a child class that inherits from Animal but overrides the speak method.Animal in Dog, unless overridden.Let’s relate this to your robotic arm scenario.
Suppose you have a library called dobot with a class Dobot for controlling the robot arm.
You want to add extra features (like a “jump” movement and timed gripper/suction control) without changing the original Dobot class.
You do this by inheriting from dobot.Dobot and adding your own methods:
class MyDobot(dobot.Dobot): # Inherit from dobot.Dobot
def jump_to(self, x, y, z, r, height=50, wait=False):
"""
Move the arm up by 'height', move horizontally, then down to target.
Useful for moving over obstacles.
"""
# Create this function by yourself
def suck(self, enable, wait_time=0):
"""
Enable/disable suction cup, optionally wait after action.
"""
self._set_end_effector_suction_cup(enable)
time.sleep(wait_time)
def grip(self, enable, wait_time=0):
"""
Enable/disable gripper, optionally wait after action.
"""
self._set_end_effector_gripper(enable)
time.sleep(wait_time)
MyDobot inherits all methods and properties from dobot.Dobot.
That means you can use everything from the original Dobot class (e.g., move_to, home, etc.) as if they were defined in MyDobot.MyDobot adds new methods (jump_to, suck with wait, grip with wait) that are not present in the base class.