Skip to main content
added 123 characters in body
Source Link
CrawlingKid
  • 994
  • 5
  • 15

You could do by the following way:

import java.util.ArrayList; import java.util.List; abstract class Parent { void callMe(){ System.out.println("Parent"); } } class Child extends Parent { void callMe(){ System.out.println("Child"); } } public class TestClass { public static void main(String[] args) { List<Parent> alist=new ArrayList<Parent>(); List<? super Child> alist2=alist; } } 

List<Parent> is not same as List<Child>. Compilor does not allow to assign the reference of List<Parent> to List<Child> even though List contains only Child Object.

For Example:

List<Parent> parentList=new ArryList<Parent>(); parentList.add(new Child()); parentList.add(new Child()); parentList.add(new Child()); //This is not allowed List<Child> childList=(List<Child>)parentList;//Compiler Error //But,This is allowed List<? super Child> childList=parentList; //Okey 

This is allowed becuase using reference of List<? super Child> gurantees that the List<Parent> will not be corrupted.

You could do by the following way:

import java.util.ArrayList; import java.util.List; abstract class Parent { void callMe(){ System.out.println("Parent"); } } class Child extends Parent { void callMe(){ System.out.println("Child"); } } public class TestClass { public static void main(String[] args) { List<Parent> alist=new ArrayList<Parent>(); List<? super Child> alist2=alist; } } 

List<Parent> is not same as List<Child>. Compilor does not allow to assign the reference of List<Parent> to List<Child> even though List contains only Child Object.

For Example:

List<Parent> parentList=new ArryList<Parent>(); parentList.add(new Child()); parentList.add(new Child()); parentList.add(new Child()); //This is not allowed List<Child> childList=(List<Child>)parentList;//Compiler Error //But,This is allowed List<? super Child> childList=parentList; //Okey 

You could do by the following way:

import java.util.ArrayList; import java.util.List; abstract class Parent { void callMe(){ System.out.println("Parent"); } } class Child extends Parent { void callMe(){ System.out.println("Child"); } } public class TestClass { public static void main(String[] args) { List<Parent> alist=new ArrayList<Parent>(); List<? super Child> alist2=alist; } } 

List<Parent> is not same as List<Child>. Compilor does not allow to assign the reference of List<Parent> to List<Child> even though List contains only Child Object.

For Example:

List<Parent> parentList=new ArryList<Parent>(); parentList.add(new Child()); parentList.add(new Child()); parentList.add(new Child()); //This is not allowed List<Child> childList=(List<Child>)parentList;//Compiler Error //But,This is allowed List<? super Child> childList=parentList; //Okey 

This is allowed becuase using reference of List<? super Child> gurantees that the List<Parent> will not be corrupted.

added 549 characters in body
Source Link
CrawlingKid
  • 994
  • 5
  • 15

You could do by the following way:

import java.util.ArrayList; import java.util.List; abstract class Parent { void callMe(){ System.out.println("Parent"); } } class Child extends Parent { void callMe(){ System.out.println("Child"); } } public class TestClass { public static void main(String[] args) { List<Parent> alist=new ArrayList<Parent>(); List<? super Child> alist2=alist; } } 

List<Parent> is not same as List<Child>. Compilor does not allow to assign the reference of List<Parent> to List<Child> even though List contains only Child Object.

For Example:

List<Parent> parentList=new ArryList<Parent>(); parentList.add(new Child()); parentList.add(new Child()); parentList.add(new Child()); //This is not allowed List<Child> childList=(List<Child>)parentList;//Compiler Error //But,This is allowed List<? super Child> childList=parentList; //Okey 

You could do by the following way:

import java.util.ArrayList; import java.util.List; abstract class Parent { void callMe(){ System.out.println("Parent"); } } class Child extends Parent { void callMe(){ System.out.println("Child"); } } public class TestClass { public static void main(String[] args) { List<Parent> alist=new ArrayList<Parent>(); List<? super Child> alist2=alist; } } 

You could do by the following way:

import java.util.ArrayList; import java.util.List; abstract class Parent { void callMe(){ System.out.println("Parent"); } } class Child extends Parent { void callMe(){ System.out.println("Child"); } } public class TestClass { public static void main(String[] args) { List<Parent> alist=new ArrayList<Parent>(); List<? super Child> alist2=alist; } } 

List<Parent> is not same as List<Child>. Compilor does not allow to assign the reference of List<Parent> to List<Child> even though List contains only Child Object.

For Example:

List<Parent> parentList=new ArryList<Parent>(); parentList.add(new Child()); parentList.add(new Child()); parentList.add(new Child()); //This is not allowed List<Child> childList=(List<Child>)parentList;//Compiler Error //But,This is allowed List<? super Child> childList=parentList; //Okey 
Source Link
CrawlingKid
  • 994
  • 5
  • 15

You could do by the following way:

import java.util.ArrayList; import java.util.List; abstract class Parent { void callMe(){ System.out.println("Parent"); } } class Child extends Parent { void callMe(){ System.out.println("Child"); } } public class TestClass { public static void main(String[] args) { List<Parent> alist=new ArrayList<Parent>(); List<? super Child> alist2=alist; } }