What is the need of Method Overloading and Overriding?

Joined
Oct 7, 2021
Messages
1
Reaction score
0
Hello,

I really can't seem to understand why method overloading and overriding is needed in java?
I have read some article regarding this but not able to catch why it's needed practically?

Also alongwith that I have read this resource, but I am not clear with this topic.

Any practical example will be appreciated.
 
Joined
Mar 3, 2021
Messages
240
Reaction score
30
They are very different concepts, so let's take them individually.

Overloading is easy: allowing different input, or input types, to a method of the same name. For the most part, this just makes the calling code much cleaner. Take, for example, a Dumper class that can print out anything you throw at it in a sensible way. Have a Dumper.dump() take an int, float, ArrayList, HashMap, etc, gives all the calling code the same interface. This can be especially useful when used with templating, where you don't always know your input type. (The alternative would be Dumper.dumpInt, dumpFloat, dumpList, dumpHash, etc. Now you've gotta run to the documentation and hope stuff is named sensibly.)

Overriding is... also easy. If you're using objects and inheritance. The parent class defines a function and a child class overrides it and provides its own implementation. This allows a child object to act like the parent, even though it's different under the hood. It's the same as implementing an Interface. You're overriding a method defined in the interface with your implementation and this allows you to place your object anywhere they're expecting the interface. We can come up with thousands of examples from the actual API, if it would help.
 
Joined
Jun 28, 2022
Messages
2
Reaction score
0
Method overloading is used to implement compile time or static polymorphism and is used to expand the readability of the program. While method overriding is used to implement runtime or dynamic polymorphism and is used to give the specific implementation of the method which is already provided by its base class.
 
Joined
Jul 1, 2022
Messages
1
Reaction score
0
Hello,

I really can't seem to understand why method overloading and overriding is needed in java?
I have read some article regarding this but not able to catch why it's needed practically?

Also alongwith that I have read this resource, but I am not clear with this topic.

Any practical example will be appreciated.
Overloading and Overriding are the concepts of object-oriented programming that are used to improve the readability and reusability of the code. Method overloading is a kind of static polymorphism. In Method overloading, we can define methods with the same name but with different parameters.

Code for Method Overloading:-


Java:
public class Sample{
   public static void add(int a, int b){
      System.out.println(a+b);
   }
   public static void add(int a, int b, int c){
      System.out.println(a+b+c);
   }
   public static void main(String args[]){
      Sample obj = new Sample();
      obj.add(20, 40);
      obj.add(40, 50, 60);
   }
}

On execution, it will produce the following output −

60
150

Method Overriding is a mechanism to achieve polymorphism where the superclass and the sub-class have the same methods, including the parameters and signature. The JVM calls the respective method depending upon the object used to call the method.

Code for Method Overriding:-


Java:
class SuperClass{
   public static void sample(){
      System.out.println("Method of the super class");
   }
}
public class SubClass extends SuperClass {
   public static void sample(){
      System.out.println("Method of the sub class");
   }
   public static void main(String args[]){
      SuperClass obj1 = new SubClass();
      SubClass obj2 = new SubClass();
      obj1.sample();
      obj2.sample();
   }
}

On execution, it will produce the following output −

Method of the super class
Method of the sub class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top