• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

generics and inheritance

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I am confused.

At https://docs.oracle.com/javase/tutorial/java/generics/inheritance.html, it states that if A <- B, then it doesn't mean that you can subsitute MyClass<B> in place of MyClass<A>. Fair enough, makes sense.

But then at https://docs.oracle.com/javase/tutorial/java/generics/unboundedWildcards.html it says that "You can insert an Object, or any subtype of Object, into a List<Object>". Why? Just because Object <- AnyClass, surely we cannot substitute MyClass<AnyClass> whenever we have MyClass<Object>.

I find the Java tutorial on Generics confusing, I really do, maybe it isn't and it is just me because I am just trying to rush it, but I find it confusing.
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The two snippets are talking about two different things.

For example, an Integer IS-A Number, so an Integer can be used in place of a Number. This means that you can add() an Integer instance into a List<Number>.

This is very different than replacing the generic itself. The second statement doesn't say that an List<Integer> and List<Number> are interchangeable. It is merely saying that an Integer can be managed by a List<Number>, as an Integer IS-A Number.

Henry
 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh. So if we have A <- B

Are you saying
MyClass<A> a = new MyClass<B>(); // is wrong, as MyClass<B> doesn't extend MyClass<A>

a.methodOfA(A myArg); // myArg can be an object of type B because B extends A
?

Is this correct?

Thanks.
 
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you tried to compile that code?
 
John Smithonian
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What happened when you tried to compile that code?



I didn't, there isn't any point in randonly compiling things without understanding the theory, but I found a tutorial that explains it well, so understand it now.

Thanks.
 
Campbell Ritchie
Marshal
Posts: 76884
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In which case your first bit of code would not have compiled (I did try it myself) because Foo is not a supertype of Foo<B> even if A is a super type of B.
It is obvious that the second code would be correct, isn't it.

Edit: Remove a < because before B it changes the text to bold. Write &lt; instead.
 
Campbell Ritchie
Marshal
Posts: 76884
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Those are the largest trousers in the world! Especially when next to this ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic