- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
While working with java programming, I am wondering whether method overloading can be done within two classes in which inheritance is implemented. These type of java questions and answers are highly searched for interview purpose, hope into get a solution of this query with an example.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There are many differences between method overloading and method overriding in java. A list of differences between method overloading and method overriding are given below:
No. | Method Overloading | Method Overriding |
---|---|---|
1) | Method overloading is used to increase the readability of the program. | Method overriding is used to provide the specific implementation of the method that is already provided by its super class. |
2) | Method overloading is performed within class. | Method overriding occurs in two classes that have IS-A (inheritance) relationship. |
3) | In case of method overloading, parameter must be different. | In case of method overriding, parameter must be same. |
4) | Method overloading is the example of compile time polymorphism. | Method overriding is the example of run time polymorphism. |
5) | In java, method overloading can't be performed by changing return type of the method only. Return type can be same or different in method overloading. But you must have to change the parameter. | Return type must be same or covariant in method overriding. |
Java Method Overloading example
- class OverloadingExample{
- static int add(int a,int b){return a+b;}
- static int add(int a,int b,int c){return a+b+c;}
- }
Java Method Overriding example
- class Animal{
- void eat(){System.out.println("eating...");}
- }
- class Dog extends Animal{
- void eat(){System.out.println("eating bread...");}
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
abcd
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same method name and parameters (i.e., method signature). One of the methods is in the parent class and the other is in the child class.
overloading vs overriding in java: https://cupofprogramming.blogspot.in/2016/10/overloading-vs-overriding-in-java.html

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page