• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Overloading & Method Overriding  RSS feed

 
Ankur Mehrotra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Friends

I am stuck with a very tricky question which neither supports overloading nor overriding, but has somewhat different output.
I am unable to understand the reason behind it's strange behavior.Please can someone help me for the same.
Below is the program











1.Now if it is overloading case , then the output should be 2 , as the method having string argument in child class is called , but it doesn't happens
2.Now if it is overriding case, then also the output should be 2, since the object of child class is being passed to the reference of parent class, but it doesn't happens
3.What actually happens is the Parent class method is called and the output comes out to be as 1, which I am unable to understand the reason.

Please anyone can help me for the same with reason.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankur Mehrotra wrote:
I am stuck with a very tricky question which neither supports overloading nor overriding, but has somewhat different output.
I am unable to understand the reason behind it's strange behavior.Please can someone help me for the same.
Below is the program


package javas.Inheritance;
public class ParentAbstractClass {
public int method1(Object o){return 1;}
}


package javas.Inheritance;
public class ChildImplClass extends ParentAbstractClass{
//@Override
public int method1(String s) { return 2;}
}

package javas.Inheritance;
public class TestClass {
public static void main (String args[]){
ParentAbstractClass p=new ChildImplClass();
System.out.println(p.method1("ankur"));
}
}



1.Now if it is overloading case , then the output should be 2 , as the method having string argument in child class is called , but it doesn't happens
2.Now if it is overriding case, then also the output should be 2, since the object of child class is being passed to the reference of parent class, but it doesn't happens
3.What actually happens is the Parent class method is called and the output comes out to be as 1, which I am unable to understand the reason.

Please anyone can help me for the same with reason.


Basically, it's overloading -- because the method signatures are different.

However, in this example, it is neither overloading or overriding -- because you are using the parent interface, so the compiler only has one method to choose from. In other words, at compile time, the compiler doesn't know about the method that takes a string.

Henry
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Henry explained... You are invoking method1() on a ParentAbstractClass reference so the complier knows that it can only call method1(Object) on that reference... Since String is a subclass of object the call is allowed.
On the other hand if you uncomment the @Override annotation in ChildImplClass the compiler catches the error since it go against the rules of overriding where the method signature must be identical (same name, type and number of arguments, and same return type only allowing covariant return types).
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I added code tags, which you should always use, and doesn't it look better now Please don't try to squeeze a whole method onto one line; 80×24 character screens are history and you don't need to save space like that.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!