I have created a child class InheritanceTest which extends parent class Parent. Parent class is an abstract class and InheritanceTest class overrides the abstract method abstractMethod(). When I write, Parent P = new InheritanceTest();, it is working fine. But when I write, List<Number> num = new ArrayList<Integer>();
, it is giving me compilation error saying that "Type mismatch: cannot convert from ArrayList<Integer>
to ArrayList<Number>
"
Note: Number is an abstract class which is extended by Integer class in jdk.
Please find below my code:
import java.util.ArrayList;
import java.util.List;
abstract class Parent {
public void test() {
System.out.println("parent version of test called");
}
public abstract void abstractMehod();
}
public class InheritanceTest extends Parent{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Number> num = new ArrayList<Integer>();
Parent parent = new InheritanceTest();
}
public void test() {
System.out.println("child version of test called");
}
@Override
public void abstractMehod() {
// TODO Auto-generated method stub
}
}
Please explain me the reason behind this behavior.