-4

I am trying understand the below code .Please help me. While reading blog i got this code.Below is the blog url http://tutorials.jenkov.com/java-generics/wildcards.html.

class A {}  
class B extends A {}

class Run {

    public static void main(String Ars[]) {

        List<A> a = new ArrayList<A>();
        List<B> b = new ArrayList<B>();

        a = b; // Compile time error

    }

}

Please tell me why i am getting compile time error.

Type mismatch: cannot convert from List<B> to List<A>
Pshemo
  • 122,468
  • 25
  • 185
  • 269
user3519807
  • 133
  • 1
  • 9
  • Start here: https://docs.oracle.com/javase/tutorial/java/generics/ – Jeffrey Bosboom Feb 18 '16 at 17:58
  • "*Please tell me why i am getting compile time error*" what error are you getting? Error message usually contains explanation/hint why it appears. Also main purpose of Stack Overflow is to be searchable repository of programming questions and answers, but if you don't post any information about error you are facing others with same problem will not be able to find your question. – Pshemo Feb 18 '16 at 18:10
  • Getting this compile time error "Type mismatch: cannot convert from List to List" – user3519807 Feb 18 '16 at 18:14
  • Please post that information in your question instead of comment (don't let future reader have to search for error message outside of main question). Also to get that error you would need to have `ClassB` first but what I see is only `B` class. And what is the purpose of having class C (you are not using it anywhere)? – Pshemo Feb 18 '16 at 18:18
  • I have added the error message to the main question. – user3519807 Feb 18 '16 at 18:24
  • I was reading this blog article.http://tutorials.jenkov.com/java-generics/wildcards.html. – user3519807 Feb 18 '16 at 18:25
  • This should give you example of why it is considered as potentially wrong code: http://stackoverflow.com/questions/7690684/java-cast-from-listb-to-lista-where-b-extends-a – Pshemo Feb 18 '16 at 18:30
  • Also please read (IMO it explains things better): [Is List a subclass of List? Why aren't Java's generics implicitly polymorphic?](http://stackoverflow.com/q/2745265/1393766) – Pshemo Feb 18 '16 at 18:30

2 Answers2

0

For an intuition, consider that if this assignment was valid, then you would be allowed to insert elements of type ClassA (via the AB reference) into a List which is actually for ClassB instances. So this assignment is disallowed.

oarfish
  • 4,116
  • 4
  • 37
  • 66
0

One problem you have in addition to what's been said in other answers is that you are missing a semi-colon.

You wrote:

AB = BC

This is missing a semicolon at the end of the line.

star2wars3
  • 83
  • 1
  • 10
  • Thank you for highlighting this. – user3519807 Feb 18 '16 at 18:04
  • 1
    @user3519807 Consider correcting rest of errors. Like fact that you are using `Class` instead of `class` (Java is case-sensitive). You are also trying to `extends ClassA` but there is no `ClassA` but class `A`. Try to counting your `{` and `}`. Also post exact error message you are facing. – Pshemo Feb 18 '16 at 18:07