Method overriding in java with same number of parameter but different return type -


class testoverride{     public int testvlaue(int a,int b){         return + b;     }      public float testvalue(int x,int y){         return x + y;     }      public float testvalue(float x){         return x;     } } public class codetester{     public static void main(string a[]){         testoverride objtest = new testoverride();         system.out.println(objtest.testvalue(2));         system.out.println(objtest.testvalue(2,3));     } } 

why output follows?

2.0 5.0 

it can take return type int instead of float return value 5?

if want overloading, must change in method signature 1 of these points:

  1. parameter types.
  2. number of parameters.
  3. order of parameters declared in method.

return type not considered 1 of method signature if have 2 methods same name , different return type compilation error, it not allowed that.

last,in code have 2 methods different name there no compilation error , call method return float

see different in vl , va

testvlaue
testvalue

you call system.out.println(objtest.testvalue(2,3)); second one


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -