{"id":28282,"date":"2022-08-28T15:03:43","date_gmt":"2022-08-28T09:33:43","guid":{"rendered":"https:\/\/python-programs.com\/?p=28282"},"modified":"2022-08-28T15:03:43","modified_gmt":"2022-08-28T09:33:43","slug":"method-overriding-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/method-overriding-in-python\/","title":{"rendered":"Method Overriding in Python"},"content":{"rendered":"
In this tutorial, let us look at what is method overriding in python along with examples, and its key features.<\/p>\n
Method overriding is a form of Run time polymorphism. The child class provides the specific implementation of the method that is already provided by the parent class. It is used to change the behavior of existing methods, and method overriding requires at least two classes. In method overriding, inheritance is always required because it occurs between methods from the parent class (superclass) and the child class (child class).<\/p>\n
parent class:<\/strong> The class that is being inherited is known as the Parent or Superclass.<\/p>\n child class:<\/strong> The class that inherits the parent class’s properties and methods is known as the Child or Subclass.<\/p>\n In Python, there are some prerequisites for method overriding. They are:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Multiple inheritance occurs when a class is derived from one or more parent(base) classes, inheriting all of its properties.<\/p>\n Overriding in Multiple Inheritance occurs when a child class provides its own implementation of methods\/attributes while inheriting them from its parent\/parents.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Features from both the base and derived classes are inherited into the new derived class in multilevel inheritance. This is comparable to a child and grandfather relationship.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" In this tutorial, let us look at what is method overriding in python along with examples, and its key features. Method overriding in Python: Method overriding is a form of Run time polymorphism. The child class provides the specific implementation of the method that is already provided by the parent class. It is used to …<\/p>\nFeatures of Method overriding<\/strong><\/h4>\n
\n
Method overriding Prerequisites\u00a0<\/strong><\/h4>\n
\n
\n
# Create a parent class say FirstClass\r\nclass FirstClass:\r\n \r\n # Create a function say display1\r\n def display1(self):\r\n print('display_1 of the FirstClass')\r\n \r\n # Create another function say display2\r\n def display2(self):\r\n print('display_2 of the FirstClass')\r\n \r\n# Create a child class say SecondClass which inherits properties from parent class, FirstClass\r\nclass SecondClass(FirstClass):\r\n \r\n # Create a function with the same name say display1 as in the parent parent\r\n # which modifies or overrides the text when it is called\r\n def display1(self):\r\n print('Overriding display1() class of FirstClass by the SecondClass')\r\n \r\n def display3(self):\r\n print('display_3 of the SecondClass')\r\n \r\n\r\n# Create an object for the child class say SecondClass() and store it in a variable\r\nsecondClass_object = SecondClass()\r\n \r\n# Call the display1() fuction of the SecondClass() which overrides the \r\n# display1() function of the FirstClass\r\nsecondClass_object.display1()<\/pre>\n
Overriding display1() class of FirstClass by the SecondClass<\/pre>\n
Method overriding with Multiple Inheritance<\/strong><\/h4>\n
\n
# Create a parent class say Vehicle\r\nclass Vehicle:\r\n # Inside the class, create a function say vehicletype\r\n def vehicletype(self):\r\n print(\"This gives the type of vehicle\")\r\n # create another function say brand\r\n def brand(self):\r\n print(\"It gives the brand of vehicle\")\r\n\r\n# Create class say Twowheeler which inherits properties \r\n# from the parent class Vehicle\r\nclass Twowheeler (Vehicle): \r\n def vehicletype(self):\r\n print(\"Bike is a type of Twowheeler\")\r\n\r\n# Create another class say Threewheeler which inherits properties \r\n# from the parent class Vehicle\r\nclass Threewheeler (Vehicle): \r\n def vehicletype(self):\r\n print(\"Auto is a type of Threewheeler\")\r\n\r\n# Create another child class say Fourwheeler which inherits properties \r\n# from the above both the child classes Twowheeler and Threewheeler \r\nclass Fourwheeler (Twowheeler, Threewheeler): \r\n def vehicletype(self):\r\n print(\"Car is a type of Fourwheeler\")\r\n \r\n# Create an object for the last child class Fourwheeler\r\nobj = Fourwheeler() \r\n\r\n# call the vehicletype() function from the Vehicle class and print the result\r\nobj.vehicletype() \r\n# call the brand() function from the Vehicle class and print the result\r\nobj.brand()<\/pre>\n
Car is a type of Fourwheeler\r\nIt gives the brand of vehicle<\/pre>\n
Method overriding with Multi-level Inheritance<\/strong><\/h4>\n
\n
# Create a parent class say Vehicle\r\nclass Vehicle:\r\n\r\n # Inside the class, create a function say vehicletype\r\n def vehicletype(self):\r\n print(\"This gives the type of vehicle\")\r\n # create another function say brand\r\n def brand(self):\r\n print(\"It gives the brand of vehicle\")\r\n\r\n# Create a child class say Twowheeler which inherits properties \r\n# from the parent class Vehicle\r\nclass Twowheeler(Vehicle): \r\n def vehicletype(self):\r\n print(\"Bike is a type of Twowheeler\")\r\n\r\n# Create another child class say Threewheeler which inherits properties \r\n# from the first child class Twowheeler\r\nclass Threewheeler(Twowheeler): \r\n def vehicletype(self):\r\n print(\"Auto is a type of Threewheeler\")\r\n\r\n# Create an object for the last child class Threewheeler\r\nobj = Threewheeler() \r\n\r\n# call the vehicletype() function from the Vehicle class and print the result\r\nobj.vehicletype() \r\n# call the brand() function from the Vehicle class and print the result\r\nobj.brand() \r\n\r\n<\/pre>\n
Auto is a type of Threewheeler\r\nIt gives the brand of vehicle<\/pre>\n