Perl | Method Overriding in OOPs
In any object-oriented programming language, Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes. When a method in a subclass has the same name, same parameters or signature and same return type(or sub-type) as a method in its super-class, then the method in the subclass is said to override the method in the super-class. Method Overriding means that the code comprises of two or more methods with the same name but each of them has a special task and that to differ from each other. Thus taking the literal meaning of the name itself it means that one method has to override another. This concept stands for redefining a base class method in its derived class with the exact same method signature. Method overriding is one of the ways by which Perl achieves Run Time Polymorphism. The version of a method that is executed will be determined by the object that is used to invoke it. If an object of a parent class is used to invoke the method, then the version in the parent class will be executed, but if an object of the subclass is used to invoke the method, then the version in the child class will be executed. In other words, it is the type of the object being referred to (not the type of the reference variable) that determines which version of an overridden method will be executed. Method overriding in Perl can be best explained with the help of the following example: We have a base class vehicle with the methods: get_mileage() and get_cost() and the derived class car with the methods: get_mileage() and get_age(). Now since one of the methods of both the classes has the same name thus their execution will take place on the principle of the Method Overriding concept. Let’s look into the example and see how they get executed.
- Creation of Base class:
- Creation of Derived Class:
- Using objects to illustrate the process of Method Overriding:
Output: As seen that the method from the class that is being called using the object overrides the other method of the same name but in the different classes. The execution of the ‘get_mileage’ method on the object vehicle prints ‘The mileage by your vehicle is: 15’ through the method which was declared in the class vehicle. Whereas when executing the ‘get_mileage’ on the object of car we get the output ‘The mileage by your car is: 10’ through the method in the class car.
Why Method Overriding?
As stated earlier, overridden methods allow Perl to support run-time polymorphism. Polymorphism is essential to object-oriented programming for one reason: it allows a general class to specify methods that will be common to all of its derivatives while allowing subclasses to define the specific implementation of some or all of those methods. Overridden methods are another way that Perl implements the “one interface, multiple methods” aspect of polymorphism. Dynamic Method Dispatch(Runtime Polymorphism) is one of the most powerful mechanisms that object-oriented design brings to bear on code reuse and robustness. The ability of existing code libraries to call methods on instances of new classes without recompiling while maintaining a clean abstract interface is a profoundly powerful tool. Overridden methods allow us to call methods of any of the derived classes without even knowing the type of derived class object. Thus method overriding makes the programming very easy as there is no need to remember different names while creating different methods instead remembering the processes within the Method is much more important.
In object-oriented programming, method overriding is the ability of a subclass to provide its own implementation of a method that is already defined in its superclass. This allows the subclass to customize the behavior of the method without modifying the original implementation.
In Perl, method overriding is accomplished by simply defining a new method with the same name in the subclass. When an object of the subclass is created and the method is called, Perl will automatically use the subclass’s implementation instead of the superclass’s implementation.
Here’s an example of method overriding in Perl:
Hello, my name is John.
Hello, my name is Mary and I’m a student.
Please Login to comment...