• Post Reply Bookmark Topic Watch Topic
  • New Topic

method overloading  RSS feed

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Parent
{
void m1(){
}
}
class Child extends Parent
{
void m1(int i){
}
};


in an interview interviewer gave this code and asked that is m1 method is overloaded
or
m1 method in Child class is treated as a new method

i told its gave the answer as not overloaded and its treated as new method

am i right?
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Can we call both m1() and m1(int) on a Child? If so, why would you not call that overloading?
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting, you are right Jeff...Here's an excerpt from JLS


8.4.9 Overloading

If two methods of a class (whether both declared in the same class, or both inherited
by a class, or one declared and one inherited
) have the same name but signatures
that are not override-equivalent, then the method name is said to be overloaded.


After all this time with Java, I can't believe I didnt know this!
 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satish bejju wrote:class Parent
{
void m1(){
}
}
class Child extends Parent
{
void m1(int i){
}
};


in an interview interviewer gave this code and asked that is m1 method is overloaded
or
m1 method in Child class is treated as a new method

i told its gave the answer as not overloaded and its treated as new method

am i right?

so your answer is wrong
Capture.PNG
[Thumbnail for Capture.PNG]
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please avoid screenshots which many people can’t read easily. Please don’t confuse such websites with definitive sources, even though the answer on the screenshot was correct. Praveen Kumar M K was correct to quote the Java™ Language Specification (JLS), which is the definitive authority. It is worth reading the whole JLS section quoted, which has some informative examples in.
 
satish bejju
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Jeff
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!