Java class generics [duplicate] - java

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.
What are my options, if I what the following code to work?
class Garage<X>{}
class Vehicle {}
class Car extends Vehicle {}
class Bike extends Vehicle {}
class A {
public static void main(String[] args) {
Garage<Car> car = new Garage<>();
Garage<Bike> bike = new Garage<>();
Garage<Vehicle> vehicle = new Garage<>();
//what are the options to do this?
vehicle=car;
vehicle=bike;
}
}
What I'm after is to try upcast(?) car and bike to vehicle. Is this even possible with this kind generic class type?

In order to do what you desire, you need to change
Garage<Vehicle> vehicle = new Garage<>();
to
Garage<? extends Vehicle> vehicle = new Garage<>();

Related

Generic variable declaration with wildChar vs without wildChar [duplicate]

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?

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.

Child Class in Superclass Array in Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is List<Dog> a subclass of List<Animal>? Why aren't Java's generics implicitly polymorphic?
I have this code:
ArrayList<A> objects = new ArrayList<A>();
objects.add(new B());
where B is a child class o A. It gives me a compile time error like so:
The method add(A) in the type ArrayList is not applicable for the arguments (B)
This compiles and runs fine:
import java.util.ArrayList;
class A {
}
class B extends A {
}
class Test {
public static void main(String[] args) {
ArrayList<A> arraylist = new ArrayList<A>();
arraylist.add(new B());
}
}
Have another look at your code. Perhaps you got it backwards and A extends B?

Categories