HOME ARTICLES ABOUT CONTACT PRIVACY POLICY
Object-Oriented Programming

Method Overriding & Overloading


December 7, 2022
Read Time: 1 mins 10 secs
Word Count: 275

In Object-Oriented Programming, methods are the operations associated with a class. Methods are defined inside a class. Methods give an object of a class its behaviour. Sending a message to an object means calling or invoking a method in the object. Methods are like functions in other programming paradigms.

What is Method Overloading

Overloading occurs in a class when there is more than one method with the same name but each has a different signature. The signature of a method is the name of the method and its parameter list including the data type, arity and order of the arguments but not the return type.If a class has several versions of a method Parent( ), for example void Parent( ), void Parent(int g), void Parent(float d, float h), then method Parent( ) is being overloaded. In Overloading the method name must be the same for overloading to be successful, the signature must be different, the return type can be overlooked, the data type of parameters, order of parameter and arity and body or contents of the method can be different.

Advantages of Method Overloading

Disadvantages of Method Overloading



Example




What is Method OverRiding

Overriding occurs when more than one method share the same name, same signature and same return type, in an inheritance hierarchy. Programmers must construct several separate method definitions in the class, each with the same name but a different list of parameters, in order to generate overloaded methods. If the parent class has a method called Login( ), and the child class also has a method called Login( ), then the child class method will override the parent class method. In Overriding the method name, signature, return type, data type of parameters, order of parameter and arity MUST be the same for overriding to be successful. The body or contents of the method can be different.


Example