Reputation: 31
There are 2 classes. Parent class has method --> public void abcd(int i)
and child class has override that -->public void abcd(Integer i)
Is this possible because same method name but int used in parent class and Integer I have been used in child class.
Practice.java
public class Practice {
public void abcd(int i){
System.out.println("Hi");
}
Practice2.java
public class Practice2 extends Practice{
public void abcd(Integer i){
System.out.println("oh child");
}
public static void main(String[] args) {
Practice p = new Practice();
Practice p2 = new Practice2();
p2.abcd(1); //Is this possible
}
}
I got this below error The method abcd() in the Practice is not applicable for the arguments(int) int and Integer are same right? why it is not accepting?
Upvotes: 2
Views: 2611
Reputation: 17474
got this below error The method abcd() in the Practice is not applicable for the arguments(int) int and Integer are same right? why it is not accepting?
It is because Integer
and int
are not the same. Integer is a class (Wrapper class for int). It may appears they are the same because Java does something known as auto-boxing unboxing for you.
Example of autoboxing:
Integer i = 5;
Example of unboxing:
int num = new Integer(5);
But in essence, they are different type. One is a class, the other is a primitive type.
Can I override parent method but with different paramter?
The meaning of override means that you are writing a different implementation for an inherited method. If you use a different method signature from its parent. How would Java knows you are trying to override the parent's method?
Hence, it is not possible to override a parent method with different parameter (method signature).
In fact, when you try to use a different parameter, you are actually doing a method overloading (i.e. writing a method with same method name by different method signature).
Upvotes: 1
Reputation: 23012
Note that, method which is inherited in child must have subsignature of parent's method signature.
Upvotes: 1
Reputation: 1
No you can not override parent method but with different parameter,If you want to override any parent your declaration will be same but you can change behavior.
Upvotes: 0
Reputation: 59
int and Integer are same right?
Integer is a wrapper class for the primitive int datatype.
Upvotes: 0
Reputation: 839
Can I override parent method but with different paramter?
No, it is not possible. You need to override the method which means that same method with different implementation, if you change the parameter or return-type it means you are changing the behavior.
int and Integer are same right?
No, they are not same. Integer is a class and int is a primitive type. See the answer for detail. here
Upvotes: 1
Reputation: 2033
Its not possible, you are mixing overloading and overriding, overriding required same method name, same arguments and same arguments type.
Upvotes: 1
Reputation: 8483
No it is not possible. According to the Java docs
To override a parent method ,child must use same signature (name, plus the number and the type of its parameters) and return type(covariant return type allowed here)
Upvotes: 5