4

Please clarify my doubt on overriding, When I am calling a method which is not overrided, the method that is being called is form parent class, please give brief explanation on this, The example is like this

   public class A {
        public void test(int x){
            System.out.println("Haiiiiiii");
        }
   }

   public class B extends A{
        public void test(Integer x){
            System.out.println("hiii Im b's method");
        }
   }

   public class Main {
        /**
         * @param args
         */
        public static void main(String[] args) {
            B a=new B();
            a.test(2);
        }
   }

I'm calling b's method but in B class the method takes wrapper class as parameter.

  • 2
    possible duplicate of [int does not override Integer in Java](http://stackoverflow.com/questions/3386039/int-does-not-override-integer-in-java) – Ankur Shanbhag Aug 25 '14 at 06:06

3 Answers3

4

There are 2 methods. One accept int and other accept Integer type. So when you call test() method first it try to find a suitable method without doing any autoboxing. In that case it can find the parent class test() method which accept an int. Therefore java will execute that.

If in case it wouldn't exist then it will try to autobox your parameter and check if there a suitable method. In that case your child class method will get execute.

Edited Java will always pick the most specific method for a type. Casting/autoboxing/unboxing only when it has to.

If you wanna call child class method you can try

a.test(new Integer(2));
  • SO, is there a way that i can call my child class test method.Like can i force for autoboxing – user3748274 Aug 25 '14 at 06:18
  • 1
    @user3748274 Yes by calling it like 'a.test( Integer.valueOf( 1 ) )'. This makes it so the compiler is now trying to find a method accepting an Integer object. If it doesn't find one it will try finding a less specific method that accepts one of Integer's parent classes. Finally it will attempt to unbox the Integer to an int and finding the most specific method for an int. – Smith_61 Aug 25 '14 at 06:19
  • Thanks boss, again onething if i create object as A a=new B(); a.test(Integer.valueOf(2)); still the parent method is calling hence it is not overriding that should call child class method – user3748274 Aug 25 '14 at 06:26
  • Check again. When you use Integer.valueOf(2) It must calling the child class method. – Thusitha Thilina Dayaratne Aug 25 '14 at 06:33
0

In this case overriding not happen. since overloading when you call it will call the method accept input argument as int.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

The compile lets you auto-box and unbox between primitives and wrappers but this doesn't make one a sub-class of the other. int and Integer are two different types hence both of them when used in the method act as two different method (as in your case)

Refer below link for a much clearer explanation int does not override Integer in Java

Community
  • 1
  • 1
Sumeet Sharma
  • 2,573
  • 1
  • 12
  • 24