Generic variable declaration with wildChar vs without wildChar [duplicate] - java

This question already has answers here:
What does the question mark in Java generics' type parameter mean? [duplicate]
(6 answers)
When to use generic methods and when to use wild-card?
(9 answers)
Closed 3 months ago.
interface Shape { }
class Circle implements Shape { }
class ShapeContainer<T extends Shape> {
T sh;
public ShapeContainer(T newInstance) {
sh = newInstance;
}
...
}
class Main {
public static void main(String[] a) {
ShapeContainer<Shape> A = new ShapeContainer(new Circle());
ShapeContainer<? extends Shape> B = new ShapeContainer(new Circle());
}
}
What are the pros and cons of declaring the variable as ShapeContainer<Shape> A vs ShapeContainer<? extends Shape> B
What condition should each one be preferred?

Related

How to don't allow Type Inference and require a explicit type on method call? [duplicate]

This question already has answers here:
How to write a generic method that takes two arguments of the same types in java?
(4 answers)
Closed 3 years ago.
I'm currently building a class with a Pair-Structure. I want to somehow guarantee that each member of the pair has the exact same type.
My question goes in the direction of: How type inference work for method calls?
Following code illustrates my problem:
public class MyClass {
public class MyPair<E> {
private E oldObj;
private E newObj;
MyPair(E pOldObj, E pNewObj) {
oldObj = pOldObj;
newObj = pNewObj;
}
}
public class MyObject {
List<MyPair<?>> listWithPairs = new ArrayList<>();
public <T> void addPair(T oldObj, T newObj) {
listWithPairs.add(new MyPair<T>(oldObj, newObj));
}
}
public static void main(String[] args) {
MyObject pairHolder = new MyObject();
pairHolder.addPair(12L, "asdf");//Call 1
pairHolder.<Long>addPair(12L, 12L);//Call 2
}
}
Is there a way in Java to make Call 1 impossible and always require a type specification like in Call 2?
perhaps by declaring your method like that way
public <T extends Number> void addPair(T oldObj, T newObj) {
listWithPairs.add(new MyPair<T>(oldObj, newObj));
}

Downcast parameter in Child class Java [duplicate]

This question already has answers here:
Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic?
(19 answers)
Closed 5 years ago.
public class Contract extends StandardEntity {
/***/
#Composition
#OnDeleteInverse(DeletePolicy.UNLINK)
#OnDelete(DeletePolicy.CASCADE)
#OneToMany(mappedBy = "contract")
protected List<Objective> objectives;
/***/
Is it possible to downgrade argument objectives in child class (ContractWthDeadLines extends Contract) from List< Objective> to List< ObjectiveWitchDeadLine>:
public class ObjectiveWitchDeadLine extends Objective
Make your Contract class generic
public class Contract<T extends Objective> extends StandardEntity {
protected List<T> objectives;
}
and if you extend it you can change the type
public class ContractDeadline extends Contract<ObjectiveWitchDeadLine> {
}

Difference between List <? extends interface> and List <interface> [duplicate]

This question already has answers here:
When do Java generics require <? extends T> instead of <T> and is there any downside of switching?
(7 answers)
Generics : List<? extends Animal> is same as List<Animal>?
(3 answers)
Closed 5 years ago.
I have this interface:
public interface IModel extends Serializable{
boolean isOnDb();
}
I have an Object that implements the interface IModel :
public class Media implements Serializable, IModel {
#Override
public boolean isOnDb() {
return isOnDb;
}
}
I want to create a List where I can put objects that implement the interface IModel. Something like this:
List<? extends IModel> list= new ArrayList<>();
Media media = new Media();
list.add(m);
the code above doesn't compile. But I have no error if I do :
List<IModel> list= new ArrayList<>();
Media media = new Media();
list.add(m);
What's the difference between List<? extends IModel> and List<IModel>?
Thank you in advance

Is there a way to send a list/map of a concrete type to a method that recieves a list/map of interfaces? [duplicate]

This question already has answers here:
Java Generics - How do I call a generic map with a successor object
(3 answers)
Closed 8 years ago.
Is there a way to send a list/map of a concrete type to a method that recieves a list/map of interfaces?
e.g.
(Toyota extends Car and Car implements ICar)
I want to call
private static void doSomething(Map<String, ICar> cars) {}
Using
Map<String, Toyota> toyotas = new HashMap<>();
doSomething0(toyotas);
You can use type bounds :
private static void doSomething(Map<String, ? extends ICar> cars) {}
Example :
public static void doSomething (Map<String,? extends Number> numMap)
{
for (Number n : numMap.values ())
System.out.println (n);
}
public static void main (String[] args)
{
Map<String,Integer> intMap = new HashMap<String, Integer> ();
intMap.put("two",2);
intMap.put("three",3);
doSomething (intMap);
}
Change your declaration to:
private static void doSomething(Map<String, ? extends ICar> cars) {}
This means acept everything that extends ICar.

Using generic interfaces [duplicate]

This question already has answers here:
What is a raw type and why shouldn't we use it?
(16 answers)
Closed 8 years ago.
Is there any differences between those two class declarations
1:
class MyClass <T extends Number & Comparable>
2:
class MyClass <T extends Number & Comparable<T>>
I think that there are differences. But I cannot find an example which would show differences because I don't understand it exact.
Can you show me this example?
There is a difference. The first one is using raw types, and thus, is less type-safe. For example:
This works, but should not work
class MyClass<T extends Number & Comparable>
{
void use(T t)
{
String s = null;
t.compareTo(s); // Works, but will cause a runtime error
}
}
Whereas this does not work (because it should not work)
class MyClass<T extends Number & Comparable<T>>
{
void use(T t)
{
String s = null;
t.compareTo(s); // Compile-time error
}
}
EDIT: Full code, as requested:
class MyClass<T extends Number & Comparable>
{
void use(T t)
{
String s = "Laziness";
t.compareTo(s); // Works, but will cause a runtime error
}
}
public class MyClassTest
{
public static void main(String[] args)
{
MyClass<Integer> m = new MyClass<Integer>();
Integer integer = new Integer(42);
m.use(integer);
}
}

Categories